[nodejs]关于http.request遇到报错BadRequestError: request aborted怎么解决
发现是content-length乜有设置成和发送的消息内容的长度一致,把长度改成消息内容的长度就行了,或者注释掉也可以。
·
今天写demo时,调用http.request方法时遇到服务端报错BadRequestError: request aborted,点击进入报错的位置:
然后根据我写请求里有content-length:

发现是content-length乜有设置成和发送的消息内容的长度一致,把长度改成消息内容的长度就行了,或者注释掉也可以。
const postMsg = JSON.stringify({'msg':'I come from demo!'})
const options = {
hostname: 'localhost',
port: 3000,
path: '/testReq',
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Content-Length':Buffer.byteLength(postMsg),
},
};
const req = request(options,(res)=>{
console.log(`STATUS: ${res.statusCode}`);
console.log(`HEADERS: ${JSON.stringify(res.headers)}`);
res.setEncoding('utf8');
res.on('data', (chunk) => {
console.log(`BODY: ${chunk}`);
});
res.on('end', () => {
console.log('No more data in response.');
});
}).on('error',(stream)=>{
console.log('连接到了=======',stream)
})
req.write(postMsg)
req.end()
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐



所有评论(0)