StormTrooper
StormTrooper

Reputation: 1573

Node Js HTTP response crashes when response length exceeds 16101

I am using Node JS HTTP request. When my response length exceeds 16101 , it truncates my response. And I recieve limited response like this:

{"id_user":"133696"},{"id_u

This is not a chunked response, its only comes once. I want to recieve the whole response instead of truncated .

My Node version is v0.10.36.

Here is my code:

var https   = require('https');
            var querystring = require('querystring');

            postData.format = 'json';
            postData.apikey = 'abcd';

            jsonObject  = querystring.stringify(postData);

            var postheaders = {
                'Content-Type'      : 'application/x-www-form-urlencoded',
                'Content-Length'    : Buffer.byteLength(jsonObject, 'utf8')
            };

            if(callMethod == undefined){
                callMethod = 'POST';
            }

            var optionspost = {
                host    : this.host,
                port    : this.port,                
                path    : this.path,                
                method  : callMethod,
                headers : postheaders
            };

            var reqPost = https.request(optionspost, function(res) {

                res.setEncoding('utf-8');               
                res.on('data', function(responseData) {

                    //---->>> responseData containes truncated response

                    if(callFunction != undefined && callFunction != null && callFunction != ''){

                        callFunction(responseData, relatedData);//****** calling success function ****
                    }
                });
                res.on('end', function() {


                });
            });

            reqPost.write(jsonObject);
            reqPost.end();
            reqPost.on('error', function(e) {
                console.error(e);
            });

Upvotes: 3

Views: 214

Answers (1)

Robert Rossmann
Robert Rossmann

Reputation: 12131

Your code is expecting the data event only once, but Node can fire it more than once. In fact, it can fire it as many times as it damn pleases.:) Every time a data event is emitted, another part of the data is provided to you. You know that there is no more data to be consumed when the end event is fired - that's where you should process the data and/or call your callbacks.

Since the response is basically a readable stream, have a look at the data event for Readable Stream.

Upvotes: 3

Related Questions