简体   繁体   中英

PhantomJs, get query parameters when a POST request is sent

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"
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM