今天写demo时,调用http.request方法时遇到服务端报错BadRequestError: request aborted,点击进入报错的位置:
然后根据我写请求里有content-length:
文章来源:https://www.toymoban.com/news/detail-529128.html
发现是content-length乜有设置成和发送的消息内容的长度一致,把长度改成消息内容的长度就行了,或者注释掉也可以。文章来源地址https://www.toymoban.com/news/detail-529128.html
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()
到了这里,关于[nodejs]关于http.request遇到报错BadRequestError: request aborted怎么解决的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!