Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get post data in chrome extension

I am trying to get post data in a simple chrome extension, but it doesn't work:

chrome.webRequest.onBeforeSendHeaders.addListener(
function(details) {
if (details.method == "POST") {
            var postData=details.requestBody.raw; 
            console.log(postData);
        }
return {requestHeaders: details.requestHeaders};
},
{urls: ["<all_urls>"]},
["blocking", "requestHeaders"]);

I am using this site to test the extension:

https://mobile.onlinesbi.com/sbidownloader/DownloadApplication.action

like image 664
adnan kamili Avatar asked Sep 08 '13 06:09

adnan kamili


1 Answers

I know this was asked so long ago, but in case anyone else comes across this same problem, I found the answer.

You're using the listener onBeforeSendHeaders, when the only listener that supports viewing the POST data is onBeforeRequest. However, you also need to supply an extraInfoSpec of "requestBody" to the third argument of .addListener. An example is below.

/* The Web Request API */
const WEB_REQUEST = chrome.webRequest;

WEB_REQUEST.onBeforeRequest.addListener(
    function(details) {
        if(details.method == "POST")
            console.log(JSON.stringify(details));
    },
    {urls: ["<all_urls>"]},
    ["blocking", "requestBody"]
);
like image 112
Garrett Avatar answered Oct 27 '22 22:10

Garrett