I'm trying to make a simple http request to a rest API via http library on nodejs. Two minutes after the call, the request triggers this error:
{ [Error: read ECONNRESET]
code: 'ECONNRESET',
errno: 'ECONNRESET',
syscall: 'read' }
The server is working because when I do this call with PHP or using Chrome's extension Advanced Rest Client it works. I don't have access to server logs or server configurations.
I'm almost sure that my call is reaching the server, because with a wrong address, I get an incorrect address error and with a different port, I get a timeout error.
var http = require('http');
var post = {
"id": msgId,
"otherdata": { "user": user }
};
var content = JSON.stringify(post);
// prepare the header
var postheaders = {
'Content-Type' : 'application/x-www-form-urlencoded',
'Content-Length' : Buffer.byteLength(content, 'utf8'),
'Accept-Encoding': 'gzip,deflate,sdch',
'Accept-Language': 'en-US,en;q=0.8'
};
// the post options
var optionspost = {
host : Constants.API_HOST,
port : Constants.API_PORT,
path : Constants.API_PATH,
method : 'POST',
headers : postheaders
};
// I found a solution that recommended adding the following code, but it didn't work either:
var keepAliveAgent = new http.Agent({ keepAlive: true });
optionspost.agent = keepAliveAgent;
// do the POST call
var request = http.request(optionspost, function(res) {
res.on('data', function(d) {
// never reached here
console.info(data);
});
res.on("end", function(){
// never reached here also
console.info(data);
});
});
request.write(content);
request.end();
request.on("error", function(e){
// ECONNRESET error is triggered here...
console.info(e);
});
I was thinking that it may be some incorrect usage of node's http library, so I was thinking about using another library for rest API calls.
Suggestions anyone?
Thanks! =)
If anyone finds this situation: My issue here was the address port. As I didn't had access to external API, I could not see the error logs, but the thing is that they moved the port and the other one was still open, but not functional. So, the server was reachable, but not responding - and that is this error: without a response, after the timeout, this error is thrown.
Thanks for the help!