expresshttp-postgzipcontent-typecontent-encoding

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 method headers: { connection: 'keep-alive', 'content-length': '1082', 'content-encoding': 'gzip', 'content-type': 'text/xml', accept: '/', 'accept-encoding': 'gzip', origin: '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() },```




Solution

  • After finding this nodejs tutorial I was able to fix it. The key is not to use any express parser, and use vanilla nodejs instead.

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