嗨,给定大量 json 对象数组,我在 NodeJS 上执行 HTTP 请求时遇到问题。鉴于 json 对象的小数组,该请求工作正常。但是,如果我尝试增加 json 的大小数组,我收到错误:socket hang up {"error":{"code":"ECONNRESET"}}。是否需要执行多次写入?还是另一端出了什么问题?
提前感谢您花时间在这里!
// data is a json object
var post_data = JSON.stringify(data);
var buf = new Buffer(post_data);
var len = buf.length;
var options = {
hostname: address,
port: port,
path: pathName,
method: 'PUT',
headers: {
'Content-Type':'application/json',
'Content-Length': len,
'Transfer-Encoding':'chunked'
}
};
// http call to REST API server
var req = restHttp.request(options, function(res) {
console.log('server PUT 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() {
callback(JSON.parse(resData));
});
});
req.write(buf);
req.end();