How to view request sent from node.js to server?

2019-07-15 03:45发布

问题:

Regarding to this question: Transfer / pass cookies from one request to another in nodejs/protractor

I got another one. How could I view complete request (headers + body) which I am performing via nodejs?

回答1:

Yes, you can ... You can access the complete request from the full response body - response.request

I have a generic full response structure illustrated below

IncomingMessage
  ReadableState
  headers(ResponseHeaders)
  rawHeaders
  request - //This is what you need
    headers 
    body
  body(Response Body)

You can access through code as shown below

var request = require("request");
var options = { method: 'POST',
    url: 'http://1.1.1.1/login',
    headers:
        {   'cache-control': 'no-cache',
            'content-type': 'application/json' },
    body: { email: 'junk@junk.com', password: 'junk@123' },
    json: true };

request(options, function (error, response, body) {
    if (error) throw new Error(error);
    // This will give you complete request
    console.log(response.request);
    //This gives you the headers from Request
    console.log(response.request.headers);
});