Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome.webRequest API - requestBody always undefined

I am trying to use the webrequest api in my Chrome extension. Using the following block of code:

$(document).ready(function(){

chrome.webRequest.onBeforeRequest.addListener(
    function(details)
    {
        console.log(details.requestBody);
    },
    {urls: ["https://myurlhere.com/*"]}
);});

The console shows me that requestBody is undefined. If I log details by itself, I can inspect the details object, but I can't seem to find requestBody object anywhere.

Is my syntax wrong? I did some searching and found a couple other examples and it seems like it should be working the way I have it. Any help is appreciated.

like image 452
spetz83 Avatar asked Aug 02 '13 13:08

spetz83


2 Answers

You must specify ['requestBody'] as the third parameter of addListener. For example:

chrome.webRequest.onBeforeRequest.addListener(
    function(details)
    {
        console.log(details.requestBody);
    },
    {urls: ["https://myurlhere.com/*"]},
    ['requestBody']
);

The documentation says:

requestBody ( optional object )

Contains the HTTP request body data.
*Only provided if extraInfoSpec contains 'requestBody'.*
like image 64
方 觉 Avatar answered Nov 13 '22 08:11

方 觉


Note that adding requestBody to addListener() would work, provided the request actually has a request body.

Most HTTP requests do not have any request body. In other words, getting undefined for e.requestBody is normal if the request does not have a request body.

like image 26
Pacerier Avatar answered Nov 13 '22 08:11

Pacerier