【问题标题】:expressjs rejecting post request (400 bad request) when content-type and content-ecoding headers are sent together当内容类型和内容编码标头一起发送时,表达js拒绝发布请求(400错误请求)
【发布时间】:2020-08-11 17:02:25
【问题描述】:
app.use( 
    express.text({type: 'text/xml'}), 
    express.json({type: 'application/json'}),
    other middlewares...) ```

发布方法头: { 连接:'保持活动', '内容长度':'1082', '内容编码':'gzip', “内容类型”:“文本/xml”, 接受:'/', '接受编码':'gzip', 来源:'chrome-extension://sxwwwagimdiliamlcqswqsw', '接受语言': '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() },```



【问题讨论】:

    标签: express http-post gzip content-type content-encoding


    【解决方案1】:

    在找到这个nodejs 教程后,我能够修复它。 关键是不要使用任何 express 解析器,而是使用 vanilla nodejs。

      app.use( 
    
            (req, res, next)=>{
                let data = []
                req.on('data', chunk => {
                    data.push(chunk)
                })
                req.on('end', () => {
                    req.body = data.toString()
                });
                next()
            }
        )
    

    【讨论】:

      猜你喜欢
      • 2017-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-13
      • 2017-01-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多