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. SQL Server错误: 0 解决方案

    1.已设置两种登录模式. 2.SQL Server配置管理器已配置好. 按Windows徽标键+R组合键,然后输入cmd. 再然后输入netsh winsock reset.接下来重启电脑,应该就可以 ...

  2. Django中使用多线程发送邮件

    1.settings.py 增加Email设置   #mail EMAIL_HOST = ‘smtp.gmail.com’                   #邮件smtp服务器 EMAIL_POR ...

  3. Spring Boot 打包分离依赖 JAR 和配置文件

    <properties> <java.version>1.8</java.version> <project.build.sourceEncoding> ...

  4. GPS坐标转化距离(短距离模型公式)

    下面是C#计算方法: class Program { const double EARTH_RADIUS = 6378.137; static void Main(string[] args) { d ...

  5. RNN,LSTM,GRU基本原理的个人理解

    记录一下对RNN,LSTM,GRU基本原理(正向过程以及简单的反向过程)的个人理解 RNN Recurrent Neural Networks,循环神经网络 (注意区别于recursive neura ...

  6. CODE【VS】 3160 最长公共子串 (后缀数组)

    3160 最长公共子串 题目描述 Description 给出两个由小写字母组成的字符串,求它们的最长公共子串的长度. 输入描述 Input Description 读入两个字符串 输出描述 Outp ...

  7. 分数拆分(刘汝佳紫书P183)

    枚举,由已知条件推得y大于k,小于等于2K AC代码: #include"iostream"#include"cstring"using namespace s ...

  8. 【04】AJAX接收服务器返回的数据

    AJAX接收服务器返回的数据 readyState 和 status 属性 readyState 属性保存有 XMLHttpRequest 对象的交互状态,从 0 到 4 变化: 0 :未初始化(还没 ...

  9. python之更加抽象 2014-4-6

    #更加抽象 12:50pm- 14:50 p112- 1.对象的魔力 多态 如count 在多种数据类型中都可以实现计数的功能 封装 对全局作用域中其他区域隐藏多余信息的原则 继承2.类和类型 创建类 ...

  10. 测试各种低价VPS

    1) dream.jp 540多的日元一个VPS,是全日本最低的VPS,但是用了以后发现最大问题是受限很多,不好用,如果你打算用作建ss或者其它***功能,对不起,请找其它VPS了 在日本dream. ...