https://stackoverflow.com/questions/18534771/chrome-extension-how-to-get-http-response-body

Chrome webrequest doesn't see POST data in requestBody

I am having a problem getting the POST data from a Facebook action. When you submit a comment on FB it posts to this url: https://www.facebook.com/ufi/add/comment/?__pc=EXP1%3ADEFAULT In the request, the post data for the post exists: Here is an example of the actual comment from the post data:

comment_text:test this is a test

When I try to access this through a Chrome Extension I can't seem to get this data. I've tried parsing the requestBody, but it is empty. I then tried to see if any of the other requestmethods would work, and I can't seem to find the data anywhere.

manifest.json:

{
"background": {
"scripts": [ "background.js" ]
},
"manifest_version": 2, "name": "Interaction Tracker",
"description": "Track social interactions by social site and customizable categories.",
"version": "1.0", "browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
},
"permissions": [ "webRequest", "webRequestBlocking", "webNavigation", "tabs", "<all_urls>", "debugger" ]
}

background.js

chrome.webRequest.onBeforeRequest.addListener(
function(details) {
if (details.method == "POST") {
var fb_add_comment_regex = new RegExp(".*facebook\.com\/ufi\/add\/comment.*");
if ( fb_add_comment_regex.test(details.url) ) {
console.log(JSON.stringify(details));
}
}
},
{urls: ["<all_urls>"]},
["blocking", "requestBody"]
); chrome.webRequest.onBeforeSendHeaders.addListener(
function(details) {
if (details.method == "POST") {
var fb_add_comment_regex = new RegExp(".*facebook\.com\/ufi\/add\/comment.*");
if ( fb_add_comment_regex.test(details.url) ) {
console.log(JSON.stringify(details));
}
}
},
{urls: ["<all_urls>"]},
["blocking", "requestHeaders"]
); chrome.webRequest.onSendHeaders.addListener(
function(details) {
if (details.method == "POST") {
var fb_add_comment_regex = new RegExp(".*facebook\.com\/ufi\/add\/comment.*");
if ( fb_add_comment_regex.test(details.url) ) {
console.log(JSON.stringify(details));
}
}
},
{urls: ["<all_urls>"]},
["requestHeaders"]
); chrome.webRequest.onHeadersReceived.addListener(
function(details) {
if (details.method == "POST") {
var fb_add_comment_regex = new RegExp(".*facebook\.com\/ufi\/add\/comment.*");
if ( fb_add_comment_regex.test(details.url) ) {
console.log(JSON.stringify(details));
}
}
},
{urls: ["<all_urls>"]},
["blocking", "responseHeaders"]
); chrome.webRequest.onAuthRequired.addListener(
function(details) {
if (details.method == "POST") {
var fb_add_comment_regex = new RegExp(".*facebook\.com\/ufi\/add\/comment.*");
if ( fb_add_comment_regex.test(details.url) ) {
console.log(JSON.stringify(details));
}
}
},
{urls: ["<all_urls>"]},
["blocking", "responseHeaders"]
); chrome.webRequest.onResponseStarted.addListener(
function(details) {
if (details.method == "POST") {
var fb_add_comment_regex = new RegExp(".*facebook\.com\/ufi\/add\/comment.*");
if ( fb_add_comment_regex.test(details.url) ) {
console.log(JSON.stringify(details));
}
}
},
{urls: ["<all_urls>"]},
["responseHeaders"]
); chrome.webRequest.onBeforeRedirect.addListener(
function(details) {
if (details.method == "POST") {
var fb_add_comment_regex = new RegExp(".*facebook\.com\/ufi\/add\/comment.*");
if ( fb_add_comment_regex.test(details.url) ) {
console.log(JSON.stringify(details));
}
}
},
{urls: ["<all_urls>"]},
["responseHeaders"]
); chrome.webRequest.onCompleted.addListener(
function(details) {
if (details.method == "POST") {
var fb_add_comment_regex = new RegExp(".*facebook\.com\/ufi\/add\/comment.*");
if ( fb_add_comment_regex.test(details.url) ) {
console.log(JSON.stringify(details));
}
}
},
{urls: ["<all_urls>"]},
["responseHeaders"]
);

Below is the output from the console.log statements for each type of request:

onBeforeRequest

{
"frameId": 0,
"method": "POST",
"parentFrameId": -1,
"requestBody": {
"raw": [
{
"bytes": { }
}
]
},
"requestId": "6724",
"tabId": 93,
"timeStamp": 1444749653166.1,
"type": "xmlhttprequest",
"url": "https:\/\/www.facebook.com\/ufi\/add\/comment\/?__pc=EXP1%3ADEFAULT"
}

onBeforeSendHeaders

{
"frameId": 0,
"method": "POST",
"parentFrameId": -1,
"requestHeaders": [
{
"name": "Origin",
"value": "https:\/\/www.facebook.com"
},
{
"name": "X-DevTools-Emulate-Network-Conditions-Client-Id",
"value": "AB63796C-002A-4670-8A56-547F8D13CA8C"
},
{
"name": "User-Agent",
"value": "Mozilla\/5.0 (Macintosh; Intel Mac OS X 10_11_0) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/45.0.2454.101 Safari\/537.36"
},
{
"name": "Content-Type",
"value": "application\/x-www-form-urlencoded"
},
{
"name": "Accept",
"value": "*\/*"
},
{
"name": "Referer",
"value": "https:\/\/www.facebook.com\/GrauZug?ref=br_rs"
},
{
"name": "Accept-Encoding",
"value": "gzip, deflate"
},
{
"name": "Accept-Language",
"value": "en-US,en;q=0.8"
},
{
"name": "Cookie",
"value": "datr=pvA2VDnTeMjGlWhVYRrnhBtO; lu=gh2TS-IuZkO-Ku-YhAzkiFIw; p=-2; c_user=100000720140344; fr=07LqqXcCamvBIa9Ww.AWU6e_qoHRglPj51gS-CF6uF-r8.BVY1Qk.DT.FYB.0.AWVvgvJA; xs=79%3A403i2b7V6bYSIA%3A2%3A1439311770%3A8344; csm=2; s=Aa4bsJIf94u-JaGr.BVyieb; presence=EDvF3EtimeF1444749507EuserFA21B00720140344A2EstateFDsb2F1444656454966Et2F_5b_5dElm2FnullEuct2F1444743344BEtrFnullEtwF2707054892EatF1444748467285G444749507665CEchFDp_5f1B00720140344F101CC; act=1444749649564%2F26"
}
],
"requestId": "6724",
"tabId": 93,
"timeStamp": 1444749653173.2,
"type": "xmlhttprequest",
"url": "https:\/\/www.facebook.com\/ufi\/add\/comment\/?__pc=EXP1%3ADEFAULT"
}

onSendHeaders

{
"frameId": 0,
"method": "POST",
"parentFrameId": -1,
"requestHeaders": [
{
"name": "Origin",
"value": "https:\/\/www.facebook.com"
},
{
"name": "X-DevTools-Emulate-Network-Conditions-Client-Id",
"value": "AB63796C-002A-4670-8A56-547F8D13CA8C"
},
{
"name": "User-Agent",
"value": "Mozilla\/5.0 (Macintosh; Intel Mac OS X 10_11_0) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/45.0.2454.101 Safari\/537.36"
},
{
"name": "Content-Type",
"value": "application\/x-www-form-urlencoded"
},
{
"name": "Accept",
"value": "*\/*"
},
{
"name": "Referer",
"value": "https:\/\/www.facebook.com\/GrauZug?ref=br_rs"
},
{
"name": "Accept-Encoding",
"value": "gzip, deflate"
},
{
"name": "Accept-Language",
"value": "en-US,en;q=0.8"
},
{
"name": "Cookie",
"value": "datr=pvA2VDnTeMjGlWhVYRrnhBtO; lu=gh2TS-IuZkO-Ku-YhAzkiFIw; p=-2; c_user=100000720140344; fr=07LqqXcCamvBIa9Ww.AWU6e_qoHRglPj51gS-CF6uF-r8.BVY1Qk.DT.FYB.0.AWVvgvJA; xs=79%3A403i2b7V6bYSIA%3A2%3A1439311770%3A8344; csm=2; s=Aa4bsJIf94u-JaGr.BVyieb; presence=EDvF3EtimeF1444749507EuserFA21B00720140344A2EstateFDsb2F1444656454966Et2F_5b_5dElm2FnullEuct2F1444743344BEtrFnullEtwF2707054892EatF1444748467285G444749507665CEchFDp_5f1B00720140344F101CC; act=1444749649564%2F26"
}
],
"requestId": "6724",
"tabId": 93,
"timeStamp": 1444749653175.2,
"type": "xmlhttprequest",
"url": "https:\/\/www.facebook.com\/ufi\/add\/comment\/?__pc=EXP1%3ADEFAULT"
}

onHeadersReceived

{
"frameId": 0,
"method": "POST",
"parentFrameId": -1,
"requestId": "6724",
"responseHeaders": [
{
"name": "status",
"value": "200"
},
{
"name": "cache-control",
"value": "private, no-cache, no-store, must-revalidate"
},
{
"name": "content-encoding",
"value": "gzip"
},
{
"name": "content-security-policy",
"value": "default-src * data: blob:;script-src *.facebook.com *.fbcdn.net *.facebook.net *.google-analytics.com *.virtualearth.net *.google.com 127.0.0.1:* *.spotilocal.com:* 'unsafe-inline' 'unsafe-eval' *.akamaihd.net *.atlassolutions.com blob: chrome-extension:\/\/lifbcibllhkdhoafpjfnlhfpfgnpldfl;style-src * 'unsafe-inline';connect-src *.facebook.com *.fbcdn.net *.facebook.net *.spotilocal.com:* *.akamaihd.net wss:\/\/*.facebook.com:* https:\/\/fb.scanandcleanlocal.com:* *.atlassolutions.com attachment.fbsbx.com 127.0.0.1:*;"
},
{
"name": "content-type",
"value": "application\/x-javascript; charset=utf-8"
},
{
"name": "date",
"value": "Tue, 13 Oct 2015 15:20:55 GMT"
},
{
"name": "expires",
"value": "Sat, 01 Jan 2000 00:00:00 GMT"
},
{
"name": "pragma",
"value": "no-cache"
},
{
"name": "public-key-pins-report-only",
"value": "max-age=500; pin-sha256=\"WoiWRyIOVNa9ihaBciRSC7XHjliYS9VwUGOIud4PB18=\"; pin-sha256=\"r\/mIkG3eEpVdm+u\/ko\/cwxzOMo1bk4TyHIlByibiA5E=\"; pin-sha256=\"q4PO2G2cbkZhZ82+JgmRUyGMoAeozA+BSXVXQWB8XWQ=\"; report-uri=\"http:\/\/reports.fb.com\/hpkp\/\""
},
{
"name": "strict-transport-security",
"value": "max-age=15552000; preload"
},
{
"name": "vary",
"value": "Accept-Encoding"
},
{
"name": "x-content-type-options",
"value": "nosniff"
},
{
"name": "x-fb-debug",
"value": "fd7Bt\/uIX2rLmngndhprmXlX3mTkZboQqcPSaw9kQt0aQUEfX3ikBMT1016i1c2RPbts9Jhbb0+bVGWPqk\/j7Q=="
},
{
"name": "x-frame-options",
"value": "DENY"
},
{
"name": "x-xss-protection",
"value": "0"
}
],
"statusCode": 200,
"statusLine": "HTTP\/1.1 200 OK",
"tabId": 93,
"timeStamp": 1444749655679.6,
"type": "xmlhttprequest",
"url": "https:\/\/www.facebook.com\/ufi\/add\/comment\/?__pc=EXP1%3ADEFAULT"
}

onResponseStarted

{
"frameId": 0,
"fromCache": false,
"ip": "31.13.93.3",
"method": "POST",
"parentFrameId": -1,
"requestId": "6724",
"responseHeaders": [
{
"name": "status",
"value": "200"
},
{
"name": "cache-control",
"value": "private, no-cache, no-store, must-revalidate"
},
{
"name": "content-encoding",
"value": "gzip"
},
{
"name": "content-security-policy",
"value": "default-src * data: blob:;script-src *.facebook.com *.fbcdn.net *.facebook.net *.google-analytics.com *.virtualearth.net *.google.com 127.0.0.1:* *.spotilocal.com:* 'unsafe-inline' 'unsafe-eval' *.akamaihd.net *.atlassolutions.com blob: chrome-extension:\/\/lifbcibllhkdhoafpjfnlhfpfgnpldfl;style-src * 'unsafe-inline';connect-src *.facebook.com *.fbcdn.net *.facebook.net *.spotilocal.com:* *.akamaihd.net wss:\/\/*.facebook.com:* https:\/\/fb.scanandcleanlocal.com:* *.atlassolutions.com attachment.fbsbx.com 127.0.0.1:*;"
},
{
"name": "content-type",
"value": "application\/x-javascript; charset=utf-8"
},
{
"name": "date",
"value": "Tue, 13 Oct 2015 15:20:55 GMT"
},
{
"name": "expires",
"value": "Sat, 01 Jan 2000 00:00:00 GMT"
},
{
"name": "pragma",
"value": "no-cache"
},
{
"name": "public-key-pins-report-only",
"value": "max-age=500; pin-sha256=\"WoiWRyIOVNa9ihaBciRSC7XHjliYS9VwUGOIud4PB18=\"; pin-sha256=\"r\/mIkG3eEpVdm+u\/ko\/cwxzOMo1bk4TyHIlByibiA5E=\"; pin-sha256=\"q4PO2G2cbkZhZ82+JgmRUyGMoAeozA+BSXVXQWB8XWQ=\"; report-uri=\"http:\/\/reports.fb.com\/hpkp\/\""
},
{
"name": "strict-transport-security",
"value": "max-age=15552000; preload"
},
{
"name": "vary",
"value": "Accept-Encoding"
},
{
"name": "x-content-type-options",
"value": "nosniff"
},
{
"name": "x-fb-debug",
"value": "fd7Bt\/uIX2rLmngndhprmXlX3mTkZboQqcPSaw9kQt0aQUEfX3ikBMT1016i1c2RPbts9Jhbb0+bVGWPqk\/j7Q=="
},
{
"name": "x-frame-options",
"value": "DENY"
},
{
"name": "x-xss-protection",
"value": "0"
}
],
"statusCode": 200,
"statusLine": "HTTP\/1.1 200 OK",
"tabId": 93,
"timeStamp": 1444749655683.3,
"type": "xmlhttprequest",
"url": "https:\/\/www.facebook.com\/ufi\/add\/comment\/?__pc=EXP1%3ADEFAULT"
}

onCompleted

{
"frameId": 0,
"fromCache": false,
"ip": "31.13.93.3",
"method": "POST",
"parentFrameId": -1,
"requestId": "6724",
"responseHeaders": [
{
"name": "status",
"value": "200"
},
{
"name": "cache-control",
"value": "private, no-cache, no-store, must-revalidate"
},
{
"name": "content-encoding",
"value": "gzip"
},
{
"name": "content-security-policy",
"value": "default-src * data: blob:;script-src *.facebook.com *.fbcdn.net *.facebook.net *.google-analytics.com *.virtualearth.net *.google.com 127.0.0.1:* *.spotilocal.com:* 'unsafe-inline' 'unsafe-eval' *.akamaihd.net *.atlassolutions.com blob: chrome-extension:\/\/lifbcibllhkdhoafpjfnlhfpfgnpldfl;style-src * 'unsafe-inline';connect-src *.facebook.com *.fbcdn.net *.facebook.net *.spotilocal.com:* *.akamaihd.net wss:\/\/*.facebook.com:* https:\/\/fb.scanandcleanlocal.com:* *.atlassolutions.com attachment.fbsbx.com 127.0.0.1:*;"
},
{
"name": "content-type",
"value": "application\/x-javascript; charset=utf-8"
},
{
"name": "date",
"value": "Tue, 13 Oct 2015 15:20:55 GMT"
},
{
"name": "expires",
"value": "Sat, 01 Jan 2000 00:00:00 GMT"
},
{
"name": "pragma",
"value": "no-cache"
},
{
"name": "public-key-pins-report-only",
"value": "max-age=500; pin-sha256=\"WoiWRyIOVNa9ihaBciRSC7XHjliYS9VwUGOIud4PB18=\"; pin-sha256=\"r\/mIkG3eEpVdm+u\/ko\/cwxzOMo1bk4TyHIlByibiA5E=\"; pin-sha256=\"q4PO2G2cbkZhZ82+JgmRUyGMoAeozA+BSXVXQWB8XWQ=\"; report-uri=\"http:\/\/reports.fb.com\/hpkp\/\""
},
{
"name": "strict-transport-security",
"value": "max-age=15552000; preload"
},
{
"name": "vary",
"value": "Accept-Encoding"
},
{
"name": "x-content-type-options",
"value": "nosniff"
},
{
"name": "x-fb-debug",
"value": "fd7Bt\/uIX2rLmngndhprmXlX3mTkZboQqcPSaw9kQt0aQUEfX3ikBMT1016i1c2RPbts9Jhbb0+bVGWPqk\/j7Q=="
},
{
"name": "x-frame-options",
"value": "DENY"
},
{
"name": "x-xss-protection",
"value": "0"
}
],
"statusCode": 200,
"statusLine": "HTTP\/1.1 200 OK",
"tabId": 93,
"timeStamp": 1444749655684.2,
"type": "xmlhttprequest",
"url": "https:\/\/www.facebook.com\/ufi\/add\/comment\/?__pc=EXP1%3ADEFAULT"
}

Any other listener not listed above didn't have output.

Anyone have any idea here?

asked Oct 13 '15 at 15:29
Artagel

265
 
    
Is requestBody really empty, i.e. are all keys missing...? – Rob W Oct 13 '15 at 16:11
    
"I've tried parsing the requestBody, but it is empty". Where's your code for that? How do you know it's empty? – rsanchez Oct 13 '15 at 16:12
1  
I'm not sure if it is empty or if they keys are missing. All I do know is that I am dumping the entire details, and this is what I get: "requestBody": { "raw": [ { "bytes": { } } ] } – Artagel Oct 13 '15 at 17:35 

Your console.log shows that requestBody is not empty.

You'll just have to parse the raw bytes:

If the request method is PUT or POST, and the body is not already parsed in formData, then the unparsed request body elements are contained in this array.

For example if the posted data is a string then you can get its value in onBeforeRequest listener:

var postedString = decodeURIComponent(String.fromCharCode.apply(null,
new Uint8Array(details.requestBody.raw[0].bytes)));

There are many methods to decode the string depending on the encoding used by the site script.

answered Oct 13 '15 at 16:22
wOxxOm

20.8k32646
 
    
Thanks. This is the correct answer. I didn't realize that there was data in the raw area. I had tried to print that data to the console but didn't realize the method. – Artagel Oct 13 '15 at 17:40
    
FYI, you seem to be missing a closing parenthesis at the end, +1 though – DelightedD0D Apr 7 at 10:42
    
Fixed, thank you. – wOxxOm Apr 7 at 10:52

chorme requestBody的更多相关文章

  1. Spring MVC之@RequestParam @RequestBody @RequestHeader 等详解

    (转自:http://blog.csdn.net/walkerjong/article/details/7946109#) 引言: 接上一篇文章,对@RequestMapping进行地址映射讲解之后, ...

  2. @RequestParam @RequestBody @PathVariable 等参数绑定注解详解

    文章主要讲解request 数据到handler method 参数数据的绑定所用到的注解和什么情形下使用. 简介: handler method 参数绑定常用的注解,我们根据他们处理的Request ...

  3. 《转载》Spring MVC之@RequestParam @RequestBody @RequestHeader 等详解

    引言: 接上一篇文章,对@RequestMapping进行地址映射讲解之后,该篇主要讲解request 数据到handler method 参数数据的绑定所用到的注解和什么情形下使用: 简介: han ...

  4. 《转载》Spring MVC之@RequestBody, @ResponseBody 详解

    引言: 接上一篇文章讲述处理@RequestMapping的方法参数绑定之后,详细介绍下@RequestBody.@ResponseBody的具体用法和使用时机: 简介: @RequestBody 作 ...

  5. IE浏览器打开chorme浏览器,如何打开其他浏览器

    看到这个标题是否感觉奇怪,为什么要用IE浏览器打开chorme或者火狐浏览器等,这个功能从开发者来说不是一个好的需求,但确实是真实存在的,有用公司的背景历史比较复杂,而且公司有过长期的开发历史,这导致 ...

  6. 令人崩溃的@requestBody乱码一例

    这个问题真是让我心力憔悴了...在客户现场对接就是乱码,StringHttpConverter怎么配置都不行... 场景其实很简单:客户那头post一个http请求,包体是json字符串,我这头spr ...

  7. SpringMVC @RequestBody接收Json对象字符串

    其实 @RequestBody接收的是一个Json对象的字符串,而不是一个Json对象.然而在ajax请求往往传的都是Json对象,后来发现用 JSON.stringify(data)的方式就能将对象 ...

  8. 利用chorme调试手机网页

    太方便了,很实用的技巧(特意记录一下) 1.pc端安装最新的chrome 2.手机端安装最新的chrome ( Android机 ) 3.USB连接线 4.打开电脑的chrome 在地址栏输入 chr ...

  9. SpringMVC@RequestBody小细节

    关于@RequestBody  参数类型自己的包装类,还是类似String/int,区别: 1.@RequestBody LoginParmar parmar String user_number = ...

随机推荐

  1. call, apply, bind 区别

    #call, apply, bind 区别及模拟实现call apply bind 三者都可以用来改变this的指向,但是在用法上略有不同  首先说一下call和apply的区别 call和apply ...

  2. 异步编程when.js

    when.js很小,压缩后只有数kb,gzip后的大小几乎可以忽略.在Node和浏览器环境里都可以使用when.js 首先,我们看一小段代码: var getData = function(callb ...

  3. mfc 菜单

    创建一个基于对话框的工程,工程名为CreateMenu 为该对话框增加一个文件菜单项和测试菜单项,如下图所示   测试菜单项至少要有一个子菜单项 在对话框属性中关联该菜单 在resource.h中增加 ...

  4. 09CSS高级定位

    CSS高级定位 定位方式——position position:static|absolute|relative static表示为静态定位,是默认设置.  absolute表示绝对定位,与下位置属 ...

  5. JavaSE-14 异常处理

    学习要点 使用try-catch-finally处理异常 使用throw.throws抛出异常 异常及其分类 log4j记录日志 异常 1  异常的定义 异常是指在程序的运行过程中所发生的不正常的事件 ...

  6. wepy.request 请求成功但是不进入success和fail方法,及请求传参问题

    1.根据wepy官方给的文档如下,用then拿后台返回的数据,如果用then报错,请先在app.wpy中配置promise. 没有success,fail,complete方法,如若用了也是不会进入方 ...

  7. css3新特性学习系列 -- border

    css3新特性 border属性(border-radius.border-image.box-shadow)详解 1.border-radius  圆角 支持:IE9+ 用法: border-rad ...

  8. 每日命令:(13)more

    more命令,功能类似 cat ,cat命令是整个文件的内容从上到下显示在屏幕上. more会以一页一页的显示方便使用者逐页阅读,而最基本的指令就是按空白键(space)就往下一页显示,按 b 键就会 ...

  9. Format 格式化函数

    转自:老百姓 Format是一个很常用,却又似乎很烦的方法,本人试图对这个方法的帮助进行一些翻译,让它有一个完整的概貌,以供大家查询之用: 首先看它的声明:function Format(const ...

  10. 杭电1722 Cake (分蛋糕)

    #include<cstdio> int f(int m,int n) { ) return n; else return f(n,m%n); } int main() { int m,n ...