Maciej Sobala Maciej Sobala - 4 years ago 138
Node.js Question

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

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?

Answer Source

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);
});
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download