【发布时间】:2015-03-24 17:38:38
【问题描述】:
我正在尝试通过 nodejs 上的 http 库向 rest API 发出简单的 http 请求。 调用后两分钟,请求触发此错误:
{ [Error: read ECONNRESET]
code: 'ECONNRESET',
errno: 'ECONNRESET',
syscall: 'read' }
服务器正在工作,因为当我使用 PHP 或使用 Chrome 的扩展程序 Advanced Rest Client 进行此调用时,它可以工作。我无权访问服务器日志或服务器配置。
我几乎可以确定我的呼叫正在到达服务器,因为地址错误,我得到一个不正确的地址错误,而使用不同的端口,我得到一个超时错误。
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);
});
我在想可能是 node 的 http 库使用不正确,所以我在考虑使用另一个库来调用 REST API。
有人建议吗?
谢谢! =)
【问题讨论】:
-
您是否尝试过使用 fiddler 发出相同的请求?在我看来,您似乎只是没有到达所需的服务器,或者它没有响应。
-
是的...我在想也许它没有对服务器产生影响...但是当我将 de url 或端口更改为 incorrect 之一时,错误更改...因此,仅当数据正确时才会出现错误(当它应该到达服务器时)...这就是为什么我认为它正在对服务器造成影响!...谢谢!
标签: node.js rest httprequest