2

I when to get the raw response include http status line and http raw headers.

expected like the curl -i --raw output

u@l:/# curl -i --raw  https://httpbin.org/ip 
HTTP/1.1 200 OK
Connection: keep-alive
Server: meinheld/0.6.1
Date: Wed, 30 Aug 2017 16:36:59 GMT
Content-Type: application/json
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
X-Powered-By: Flask
X-Processed-Time: 0.000962972640991
Content-Length: 31
Via: 1.1 vegur

{
  "origin": "11.52.71.4"
}

Now I Stitching string like this

const request = require('request');

request('https://httpbin.org/get', function (err, rsp, body) {
  var headers = '';
  for (var i = 0; i < rsp.rawHeaders.length; i++) {
    headers += rsp.rawHeaders[i] + (i % 2 ? '\r\n' : ': ');
  }

  console.log('HTTP/' + rsp.httpVersion + ' ' + rsp.statusCode + ' ' + rsp.statusMessage
    + '\r\n' + headers + '\r\n' + rsp.body);
});

It's work, but is there easy way to get the rawResponse?

consatan
  • 349
  • 1
  • 7
  • 18
  • I don't think nodejs http server retains the raw request. It parses and puts it into the request object where you can access the pieces. What is the need for the raw request so maybe we could offer a different solution? – jfriend00 Aug 31 '17 at 02:25
  • @jfriend00 I try set different proxy in each tab when I used chrome devTools protocol.(like [Intercept and block requests by URL](https://github.com/cyrus-and/chrome-remote-interface/wiki/Intercept-and-block-requests-by-URL)) The event [`Network.continueInterceptedRequest`](https://chromedevtools.github.io/devtools-protocol/tot/Network/#method-continueInterceptedRequest) must be set a `rawResponse` param – consatan Aug 31 '17 at 03:47
  • I have no idea what that means. If you're talking about Chrome tabs, I don't see how that has anything to do with getting a rawResponse in nodejs. That sounds like something you are trying to do in a browser add-on which would not be a node.js environment at all. – jfriend00 Aug 31 '17 at 04:19
  • This sounds like an XY problem (http://xyproblem.info/) – shaochuancs Aug 31 '17 at 09:30
  • Possible duplicate of [Read raw http message in Nodejs](https://stackoverflow.com/questions/21428036/read-raw-http-message-in-nodejs) – shaochuancs Aug 31 '17 at 09:43

0 Answers0