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.
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'.*
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With