question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using bodyParser type option with content-type header

See original GitHub issue

I 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:closed
  • Created 7 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
dougwilsoncommented, Aug 10, 2016

Hey @getupandgo, it’s no problem 😃 As for your type: 'raw' issue, this is because body-parser will take what you gave as type and hands it to type-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:

app.use(bodyParser.raw({
  type: function (req) { return req.headers['content-type'] === 'raw' },
  limit: config.proxy.bodyparserlimit
}))
0reactions
dougwilsoncommented, Sep 18, 2016

Awesome, good to hear!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found