1

I'm using node.js (10.23) https lib to send request to a 3rd party web service. Everything appears ok, but recently I found a weird issue which has confused me.

Here is the code:

var https = require('https');        

function callWebService(requestOpts,body,accessToken,callback){
    var options = {
        host: requestOpts.host, 
        path: requestOpts.path,
        method: 'POST',
        headers: {
            'Content-Type':'application/json',
            'Content-Length': Buffer.byteLength(body),  
            'Authorization':'Bearer '+accessToken
        }
    };

    var req = https.request(options, function(res) {
        logger.log('STATUS: ' + res.statusCode);
        logger.log('HEADERS: ' + JSON.stringify(res.headers));

        if(res.statusCode!='200') {
            var error = new Error("Web service call failed");
            return callback(error);           
        } else {
            logger.log('web service call success');
            return callback();
        }   
    });

    req.on('error', function(e) {
        logger.error('call web service,error occurs: ' + e.message);
        return callback(e);     
    });
    req.write(body);
    req.end();
}

Sometimes (not every time) I can see both success and errors in the logs, the error is 'read ECONNRESET'. I don't really know why this occurs.

From my view, a http(s) transaction is one request and one response, how could it be one request, but get possible two responses.

And I checked node.js official document, it says

If any error is encountered during the request (be that with DNS resolution, TCP level errors, or actual HTTP parse errors) an 'error' event is emitted on the returned request object.

So I guess that maybe the http(s) request and response work fine and the error happens on 'TCP level', but why is the call back is called twice?

Metalskin
  • 3,998
  • 5
  • 37
  • 61
Neo
  • 123
  • 1
  • 9
  • 1
    Is it possible to add the complete log you got after one request (which is causing the problem of course)? –  Jan 22 '14 at 05:12
  • this is full error log,it's very simple.error occurs: read ECONNRESET { [Error: read ECONNRESET] code: 'ECONNRESET', errno: 'ECONNRESET', syscall: 'read' } – Neo Jan 23 '14 at 07:06
  • Did some research on this issue, seems it's node's bug,but not for sure.https://github.com/joyent/node/issues/5119 https://github.com/joyent/node/issues/5360 – Neo Jan 23 '14 at 07:07
  • Yeah. The issue is because of lack of TLS support in IIS servers. But still this does not resolve the issue of 2 responses. Perhaps, I didn't put my request properly. I was more interested in the order of appearance of logs rather than the full log message. –  Jan 23 '14 at 09:18

0 Answers0