Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resolve NODE.Js HTTP POST "ECONNRESET" Error

I have this function and the below data which is passed into this function returns a ECONNRESET, socket hang up error. However, when the discountCode array is reduced to like only 10 objects, it can POST without any problem.

What could the cause for this problem? I tried to do multiple req.write() by segmenting the data in Buffer, however that doesn't work out well. Any NodeJs ninja could give some insights to this problem?

createObj: function(data, address, port, callback) {

//console.log('Create Reward: '+JSON.stringify(data));
var post_data = JSON.stringify(data);

var pathName = '/me/api/v1/yyy/'+data.idBusinessClient+'/newObj';

    // 
    var options = {
        hostname: address,
        port: port,
        path: pathName,
        method: 'POST',
        headers: {
            'Content-Type': 'application/json; charset=utf-8',
            'Accept': 'application/json',
            'Accept-Encoding': 'gzip,deflate,sdch',
            'Accept-Language': 'en-US,en;q=0.8'
        }
    };

    // http call to REST API server
    var req = restHttp.request(options, function(res) {

        console.log('HTTP API server PUT Reward response received.');
        var resData = '';
        res.on('data', function(replyData) {

            // Check reply data for error.
            console.log(replyData.toString('utf8'));
            if(replyData !== 'undefined')
                resData += replyData;
        });

        res.on('end', function() {
            //<TODO>Process the data</TODO>             
            callback(JSON.parse(resData));
        });
    });

    req.write(post_data);
    req.end();

    console.log('write end');

    req.on('close', function() {
        console.log('connection closed!');
    });

    req.on('error', function(err) {
        console.log('http request error : '+err);
        callback({'error':err});
        throw err;
    });

    req.on('socket', function(socket) {
        console.log('socket size:'+socket.bufferSize);
        socket.on('data', function(data) {
            console.log('socket data:'+data);
        });
    });

}

]}`

like image 456
Hong Zhou Avatar asked Nov 06 '13 11:11

Hong Zhou


1 Answers

I had the same problem and was able to resolve it by adding a Content-Length header:

    headers: {
        'Content-Type': 'application/json; charset=utf-8',
        'Content-Length': Buffer.byteLength(post_data),
        'Accept': 'application/json',
        'Accept-Encoding': 'gzip,deflate,sdch',
        'Accept-Language': 'en-US,en;q=0.8'
    }

However, I still have no clear idea why a missing Content-Length header causes such a trouble. I assume it's some kind of weirdness in the internal Node.js code. Maybe you can even call it a bug, but I'm not sure about that ;)

PS: I'm absolutely interested more information about the cause of this problem. So please leave a comment if you have any idea...

like image 137
Torben Avatar answered Oct 23 '22 00:10

Torben