当 content-type 和 content-ecoding headers 一起发送时,expressjs 拒绝 post 请求(400 个错误请求)
expressjs rejecting post request (400 bad request) when content-type and content-ecoding headers are sent together
app.use(
express.text({type: 'text/xml'}),
express.json({type: 'application/json'}),
other middlewares...) ```
Post 方法 headers:
{
连接:'keep-alive',
'content-length': '1082',
'content-encoding': 'gzip',
'content-type': 'text/xml',
接受:'/',
'accept-encoding': 'gzip',
来源:'chrome-extension://sxwwwwagimdiliamlcqswqsw',
'accept-language': 'fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7'
}
Also I have tried express.raw with a wildcard for the type, but the response
is always 400.
```express.raw({type:'*/*', inflate:true}), (req, res, next)=>{console.log(req.body); next() },```
找到这个 nodejs 教程后,我能够修复它。
关键是不要使用任何快速解析器,而是使用普通的 nodejs。
app.use(
(req, res, next)=>{
let data = []
req.on('data', chunk => {
data.push(chunk)
})
req.on('end', () => {
req.body = data.toString()
});
next()
}
)
app.use(
express.text({type: 'text/xml'}),
express.json({type: 'application/json'}),
other middlewares...) ```
Post 方法 headers: { 连接:'keep-alive', 'content-length': '1082', 'content-encoding': 'gzip', 'content-type': 'text/xml', 接受:'/', 'accept-encoding': 'gzip', 来源:'chrome-extension://sxwwwwagimdiliamlcqswqsw', 'accept-language': 'fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7' }
Also I have tried express.raw with a wildcard for the type, but the response
is always 400.
```express.raw({type:'*/*', inflate:true}), (req, res, next)=>{console.log(req.body); next() },```
找到这个 nodejs 教程后,我能够修复它。 关键是不要使用任何快速解析器,而是使用普通的 nodejs。
app.use(
(req, res, next)=>{
let data = []
req.on('data', chunk => {
data.push(chunk)
})
req.on('end', () => {
req.body = data.toString()
});
next()
}
)