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. ActiveX控件获取不到对象属性或者方法的原因分析

    1.找不到调用的DLL或程序: 2.调用控件方法名称,与定义的函数名称不符合: 3.如果是网站网页调用ActiveX,检查控件是否添加安全对象: 4.如果是网站网页调用ActiveX,检查网页是否加入 ...

  2. 【C语言】控制台窗口图形界面编程(五):文本移动

    目录 00. 目录 01. CHAR_INFO结构 02. ScrollConsoleScreenBuffer函数 03. 程序示例 04. 官方参考程序 00. 目录 01. CHAR_INFO结构 ...

  3. hdfs深入:04、hdfs当中的元数据管理以及元数据节的查看

    6.HDFS的元数据信息FSimage以及edits和secondaryNN的作用 fsimage:存放的是一份最完整的元数据信息,内容比较大edits:元数据操作日志,记录了一段时间的元数据信息的变 ...

  4. 关于Python构建微服务的思考(一)

    一:什么是微服务? 微服务是一种架构风格,一个大型复杂软件应用由一个或多个微服务组成. 系统中的各个微服务可被独立部署,各个微服务之间是松耦合的. 每个微服务仅关注于完成一件任务并很好地完成该任务. ...

  5. 程序包javax.servlet.http不存在

    在maven test项目时,出现错误: java:[7,26] 程序包javax.servlet.http不存在 原因:pom.xml中未引入javax.servlert-api相关的包 <d ...

  6. LeetCode(36)Valid Sudoku

    题目 Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...

  7. Spring Boot 2 (三):Spring Boot 开源软件都有哪些?

    016年 Spring Boot 还没有被广泛使用,在网上查找相关开源软件的时候没有发现几个,到了现在经过2年的发展,很多互联网公司已经将 Spring Boot 搬上了生产,而使用 Spring B ...

  8. Swift - 修改导航栏“返回”按钮文字,图标

    Swift - 修改导航栏“返回”按钮文字,图标 2015-11-27 09:13发布:hangge浏览:4037   项目中常常会使用 UINavigationController 对各个页面进行导 ...

  9. Python+selenium下拉菜单选项

    案例:在我要自学网登录页面选择要保留的时间 具体页面如图所示: 使用前端工具查看部分页面代码: <select class="loinp" name="Cookie ...

  10. 计算1+2+...+100之和<for循环的学习>

    #include <stdio.h> /* 计算1+2+....+100 soulsjie 20170525 */ void main(){ int i; int s=0; for(i=0 ...