1

I'm using onResourceRequested to listen for every resource requested. When the resource is request using GET method, I can get the query parameters from the url.

But when it is a POST request, how do I retrieve the parameters(such as form data) sent with the request?

The requestData object has the following keys: headers,id,method,time,url.

page.onResourceRequested = function (requestData, networkRequest) {

        if(requestData.method == "POST")
            console.log('Receive ' + JSON.stringify(requestData, undefined, 4));
};
Receive {
    "headers": [
        {
            "name": "Origin",
            "value": "https://mastec.taleo.net"
        },
        {
            "name": "User-Agent",
            "value": "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.204 Safari/534.16"
        },
        {
            "name": "Content-Type",
            "value": "application/x-www-form-urlencoded"
        },
        {
            "name": "Referer",
            "value": "https://example.com?lang=en"
        },
        {
            "name": "Accept",
            "value": "*/*"
        },
        {
            "name": "Content-Length",
            "value": "20161"
        }
    ],
    "id": 18,
    "method": "POST",
    "time": "2015-07-16T14:03:54.838Z",
    "url": "https://example3.com"
}
Artjom B.
  • 61,146
  • 24
  • 125
  • 222
simplyblue
  • 2,279
  • 8
  • 41
  • 67
  • 1
    We had a similar issue, but noticed that the build 20160112 has an extra field `postData` for POST requests. It's not documented though. – adifire Jul 27 '16 at 18:38

1 Answers1

1

Generally, you can't. PhantomJS doesn't expose a way to get the contents of any request.

If you want to look into the request that is generated from an old-HTML (non-AJAX version) form submit, then you would need to scrape the form fields before submitting to get the contents.

If it is AJAXy, then there are three strategies that you can try. I've described them in my answer here. The general solution would be to write an XHR object proxy.

Community
  • 1
  • 1
Artjom B.
  • 61,146
  • 24
  • 125
  • 222
  • Does it also work for plain Http Requests, for Eg: if a `form` is submitted with `POST` method? , not through Ajax XHR. If not are you aware of any way to capture it. – simplyblue Jul 16 '15 at 15:40
  • No, it's not possible. You could use a proxy or a tool such as tcpdump. – Artjom B. Jul 16 '15 at 17:02