node.jsrequestjsnodejs-stream

8192 limited data size in NodeJS request


I'm trying to fetch json from an api but only half of the response is received. So how to get the full response?

var request = require('request');
var url_to_check = 'http://example.com/api/test';

request.get(url_to_check).on('data', function(data) {

// Only half of the data is printed (8192). Remaining bytes are lost.

    console.log(data.toString());

})

Solution

  • Your code is correct the only mistake you made is that you are streaming request data so you won't get whole data on event 'data' if a response is large. You will have to collect chunk and consolidate on event 'end'. check this code snippet

    var request = require('request');
    var url = 'https://reqres.in/api/users';
    var req = request.get(url)
    var data = []
     req.on('data',function(chunk){
       data.push(chunk))
     })
     req.on('end',function(){
       console.log(Buffer.concat(data).toString())
     })
    

    And If you don't want to stream and pipe data and also response size is small then you can try this:

        request.get(url, function(err, response, responseBody) {
        if (!err) {
            // var jsonRes = JSON.parse(JSON.stringify(response))
            // responseBody = jsonRes.body
            console.log(responseBody)
        } else {
            // handle error here
        }
    })