Hon*_*hou 5 javascript json http node.js
我有这个函数,下面的数据传递给这个函数返回一个ECONNRESET,套接字挂起错误.但是,当discountCode数组减少到只有10个对象时,它可以没有任何问题POST.
这个问题的原因是什么?我尝试通过分割Buffer中的数据来执行多个req.write(),但是这样做效果不好.任何NodeJs忍者都可以对这个问题有所了解吗?
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);
});
});
}
Run Code Online (Sandbox Code Playgroud)
]}`
我有同样的问题,并能够通过添加Content-Length标头来解决它:
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'
}
Run Code Online (Sandbox Code Playgroud)
但是,我仍然不清楚为什么缺少Content-Length标头会导致这样的麻烦.我认为它在内部Node.js代码中有些奇怪.也许你甚至可以把它称为bug,但我不确定;)
PS:我非常感兴趣了解有关此问题原因的更多信息.如果您有任何想法,请发表评论......
| 归档时间: |
|
| 查看次数: |
15893 次 |
| 最近记录: |