spetz83 spetz83 - 4 months ago 47
Javascript Question

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.

Answer

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'.*