ajsie ajsie - 10 days ago 4
HTTP Question

Get the whole response body when the response is chunked?

I'm making a HTTP request and listen for "data":

response.on("data", function (data) { ... })


The problem is that the response is chunked so the "data" is just a piece of the body sent back.

How do I get the whole body sent back?

Answer
request.on('response', function (response) {
  var body = '';
  response.on('data', function (chunk) {
    body += chunk;
  });
  response.on('end', function () {
    console.log('BODY: ' + body);
  });
});
request.end();
Comments