chorme requestBody

Chrome webrequest doesn‘t see POST data in requestBody

Ask Question


up vote1down votefavorite

1

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?

javascript google-chrome-extension


shareimprove this question

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 

add a comment

1 Answer

activeoldestvotes


up vote7down voteaccepted

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.


shareimprove this answer

edited Apr 7 at 10:51

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

时间: 2024-10-16 00:03:51

chorme requestBody的相关文章

spring @RequestBody的使用

@RequestMapping(value="/testJson",produces="application/json") public String testJson(@RequestBody AccountTest test){ System.out.println(test.getUsername()); return "index"; } 研究了下这个的使用: 如果http请求body传的是json的话就得使用他来接受了,可以直接用St

【转】@RequestParam @RequestBody @PathVariable 等参数绑定注解详解

@RequestParam @RequestBody @PathVariable 等参数绑定注解详解 2014-06-02 11:24 23683人阅读 评论(2) 收藏 举报 目录(?)[+] 引言: 接上一篇文章,对@RequestMapping进行地址映射讲解之后,该篇主要讲解request 数据到handler method 参数数据的绑定所用到的注解和什么情形下使用: 简介: handler method 参数绑定常用的注解,我们根据他们处理的Request的不同内容部分分为四类:(主

SpringMVC @RequestBody接收Json对象字符串

转:http://www.cnblogs.com/quanyongan/archive/2013/04/16/3024741.html 以前,一直以为在SpringMVC环境中,@RequestBody接收的是一个Json对象,一直在调试代码都没有成功,后来发现,其实 @RequestBody接收的是一个Json对象的字符串,而不是一个Json对象.然而在ajax请求往往传的都是Json对象,后来发现用 JSON.stringify(data)的方式就能将对象变成字符串.同时ajax请求的时候也

@RequestParam @RequestBody @PathVariable 等参数绑定注解详解(转)

简介: handler method 参数绑定常用的注解,我们根据他们处理的Request的不同内容部分分为四类:(主要讲解常用类型) A.处理requet uri 部分(这里指uri template中variable,不含queryString部分)的注解:   @PathVariable; B.处理request header部分的注解:   @RequestHeader, @CookieValue; C.处理request body部分的注解:@RequestParam,  @Reque

spring 中注解的详细解释之@Responsebody与@RequestBody

1.@responseBoby @Responsebody与@RequestBody 一.预备知识:@RequestMapping RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上.用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径. 1 @RequestMapping(value = "/aaa")//类级别,可以没有 2 public class myController { 3 @RequestMapping(value = "/

在SpringMVC中使用@RequestBody和@ResponseBody注解处理json时,报出HTTP Status 415的解决方案

我在使用SpringMVC的@RequestBody和@ResponseBody注解处理JSON数据的时候,总是出现415的错误,说是不支持所提交数据格式,我在页面中使用了JQuery的AJAX来发出JSON数据给服务器: $.ajax({ type:'post', url:'${pageContext.request.contextPath }/requestJSON.action', contentType :'application/json;charset=utf-8', //数据是JS

调用Request对象的post方法把RequestBody传入进去

通过调用登录接口发送一个post请求.跟get不一样的地方就是传参数不一样,post请求需要把参数封装到RequestBody对象,调用Request对象的post方法把RequestBody传入进去.最后调用execute方法执行请求,这个方法前面get请求的时候讲过. http://bbs.guilinlife.com/thread-9125238-1-1.htmlhttp://bbs.guilinlife.com/thread-9125240-1-1.htmlhttp://bbs.guil

chorme和IE下的滚动条样式修改

火狐下的滚动条样式无法去修改,但chorme下的则可以任意修改,惊喜的是IE竟然是最早实现这一功能的浏览器,IE5都能有效果. chorme下的滚动条样式修改: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style type="text/css&q

@RequestBody

作用: i) 该注解用于读取Request请求的body部分数据,使用系统默认配置的HttpMessageConverter进行解析,然后把相应的数据绑定到要返回的对象上: ii) 再把HttpMessageConverter返回的对象数据绑定到 controller中方法的参数上. 使用时机:A) GET.POST方式提时, 根据request header Content-Type的值来判断: application/x-www-form-urlencoded, 可选(即非必须,因为这种情况