Get the whole body of the response when the answer is broken?

I make an HTTP request and listen to the "data":

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

The problem is that the answer is fragmented, so the β€œdata” is just part of the body sent back.

How do I get my whole body back?

+43
Feb 22 2018-11-21T00:
source share
6 answers
 request.on('response', function (response) { var body = ''; response.on('data', function (chunk) { body += chunk; }); response.on('end', function () { console.log('BODY: ' + body); }); }); request.end(); 
+62
Feb 22 2018-11-21T00:
source share

At https://groups.google.com/forum/?fromgroups=#!topic/nodejs/75gfvfg6xuc Tane Piper offers a good solution, very similar to scriptfromscratch, but for the case of a JSON response:

  request.on('response',function(response){ var data = []; response.on('data', function(chunk) { data.push(chunk); }); response.on('end', function() { var result = JSON.parse(data.join('')) return result }); });` 

This refers to the issue that the OP exposes in the comment section of the scriptfromscratch response.

+16
Feb 01 '13 at 9:21
source share

I have never worked with the HTTP-Client library, but since it works the same as the server API, try something like this:

 var data = ''; response.on('data', function(chunk) { // append chunk to your data data += chunk; }); response.on('end', function() { // work with your data var }); 

Refer to node.js docs for help.

+5
Feb 22 2018-11-21T00:
source share

The reason it got messed up is because you need to call JSON.parse (data.toString ()). Data is a buffer, so you cannot just parse it.

+3
Jan 14 '14 at 13:00
source share

To support the full range of possible HTTP applications, Node.js HTTP APIs are very low-level. Thus, the data does not get a piece by piece in general.
There are two approaches to this problem:

1) Collect data from several β€œdata” events and add results
together before printing the output. Use the "end" event to determine when the thread is finished, and you can write the output.

 var http = require('http') ; http.get('some/url' , function (resp) { var respContent = '' ; resp.on('data' , function (data) { respContent += data.toString() ;//data is a buffer instance }) ; resp.on('end' , function() { console.log(respContent) ; }) ; }).on('error' , console.error) ; 

2) Use a third-party package to abstract the difficulties associated with the use of collecting the entire data stream. Two different packages provide a useful API to solve this problem (most likely!): Bl (Buffer
List) and concat-stream; choose!

 var http = require('http') ; var bl = require('bl') ; http.get('some/url', function (response) { response.pipe(bl(function (err, data) { if (err) { return console.error(err) } data = data.toString() ; console.log(data) ; })) }).on('error' , console.error) ; 
+3
Nov 23 '16 at 13:33
source share

If you do not mind using a query library

 var request = require('request'); request('http://www.google.com', function (error, response, body) { if (!error && response.statusCode == 200) { console.log(body) // Print the google web page. } }) 
+1
Nov 10 '14 at 1:37
source share



All Articles