Chrome.webRequest API - requestBody is always undefined

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

$(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 the data myself, I can check the part object, but I cannot find the requestBody object anywhere.

Is my syntax wrong? I did a few searches and found a couple of other examples, and it seems that it should work the way I am. Any help is appreciated.

+4
source share
2 answers

You must specify ['requestBody'] as the third parameter to addListener. For instance:

 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'.* 
+8
source

Note that adding requestBody to addListener() will work, provided , in fact the request has a request body.

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

+1
source

Source: https://habr.com/ru/post/953420/


All Articles