Using bodyParser type option with content-type header
See original GitHub issueI need to use bodyParser.raw parser only when req.headers.content-type is ‘raw’ (because app must handle many requests with different types). But when i define this header in postman and then trying to use bodyParser like this i’m getting an empty req.body:
app.use(bodyParser.raw({
type: 'raw',
limit: config.proxy.bodyparserlimit
}))
app.use(bodyParser.json({
limit: config.proxy.bodyparserlimit
}))
app.use(bodyParser.urlencoded({
limit: config.proxy.bodyparserlimit,
extended: true
}))
app.post('/', upload.any(), function (req, res) {
var requestData = req.body
requestData.contentType = req.headers['content-type']
}
Seems like bodyParser trying to find type field in req.body, but there is nothing there. So how can I use type option with Content-Type header?
Issue Analytics
- State:
- Created 7 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Express body-parser middleware
Returns middleware that only parses json and only looks at requests where the Content-Type header matches the type option. This parser accepts any...
Read more >Express body-parser supports both Content-Type 'text/plain ...
Is there an option in bodyParser that supports both 'text/plain' and 'application/json'? router.post('/action', bodyParser.json(), async (req, ...
Read more >ContentTypeParser - Fastify
For OPTIONS and DELETE requests the payload is only parsed if the content type is given in the content-type header. If it is...
Read more >Scala Body Parsers - 2.8.x - Play Framework
The default body parser that's used if you do not explicitly select a body parser will look at the incoming Content-Type header, and...
Read more >node_modules/body-parser - PLMlab
bodyParser.json([options]) ... Returns middleware that only parses json and only looks at requests where the Content-Type header matches the type option. This ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hey @getupandgo, it’s no problem 😃 As for your
type: 'raw'
issue, this is becausebody-parser
will take what you gave as type and hands it totype-is
module (see docs https://github.com/expressjs/body-parser#type). If you give it a simple string, it looks like what the MIME type is for that file extension. In this case, it tries to use whatever the file.raw
is, which does not exist, and ends up never matching any requests at all.If you really want to match the header
Content-Type: raw
, you should just use a function and do it manually. Example:Awesome, good to hear!