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

梦想与她 提交于 2019-12-24 02:42:56

问题


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);
});


来源:https://stackoverflow.com/questions/42078810/how-to-view-request-sent-from-node-js-to-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!