今天寫demo時(shí),調(diào)用http.request方法時(shí)遇到服務(wù)端報(bào)錯(cuò)BadRequestError: request aborted,點(diǎn)擊進(jìn)入報(bào)錯(cuò)的位置:
?然后根據(jù)我寫請(qǐng)求里有content-length:
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-529128.html
?發(fā)現(xiàn)是content-length乜有設(shè)置成和發(fā)送的消息內(nèi)容的長(zhǎng)度一致,把長(zhǎng)度改成消息內(nèi)容的長(zhǎng)度就行了,或者注釋掉也可以。文章來(lái)源地址http://www.zghlxwxcb.cn/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()
到了這里,關(guān)于[nodejs]關(guān)于http.request遇到報(bào)錯(cuò)BadRequestError: request aborted怎么解決的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!