Comments (4)
I mean, the example works just fine. The req.body
check is in there simply as a precaution, because you can never be too careful...
If the user does not set a Content-Type header at all, how do I default to application/json so I always get a populated object instead of an empty one?
This doesn't make any sense, because if there is no Content-Type
, the user is 100% doing something wrong. The answer here is they need to send a content type :)
Or at the very least how do I tell them they are missing that Content-Type header? If the Content-Type is set, how do I determine that it is not correct and send them an appropriate error?
This is a problem that will be solved in 2.0, but for now, this is what you can do:
var bodyParser = require('body-parser')
var createError = require('http-errors')
var express = require('express')
var onFinished = require('on-finished')
var app = express()
// create application/json parser
var jsonParser = bodyParser.json()
// create application/x-www-form-urlencoded parser
var urlencodedParser = bodyParser.urlencoded({ extended: false })
// POST /login gets urlencoded bodies
app.post('/login', singularBodyType(urlencodedParser), function (req, res) {
if (!req.body) return res.sendStatus(400)
res.send('welcome, ' + res.body.username)
})
// POST /api/users gets JSON bodies
app.post('/api/users', singularBodyType(jsonParser), function (req, res) {
if (!req.body) return res.sendStatus(400)
// create user in req.body
})
function singularBodyType(parser) {
return function parseBody(req, res, next) {
if (!req.headers['content-type']) {
return next(createError(415, 'missing content-type'))
}
parser(req, res, function (err) {
if (err) return next(err)
if (!onFinished.isFinished(req)) {
return next(createError(415, 'unsupported content-type header'))
}
next()
})
}
}
from body-parser.
P.S. I have been toying with a way to be able to automatically send those kinds of 415s in the future as well. If you have an idea for a good way, feel free to send a PR :)
from body-parser.
Sorry, I am new to node and express, and I have tried the "express route-specific" with express router, but it doesn't work. Is it correct behavior? if so, is there any solution that I can use the body-parser for specific route in router?
from body-parser.
Hi @chandara , I'd be happy to help. Please open a new issue at https://github.com/expressjs/body-parser/issues/new and explain your question, including the definition of "it doesn't work" and we'll proceed from there :)
from body-parser.
Related Issues (20)
- bodyParser is deprecated, error HOT 1
- bodyparser.json() shown as deprecated? (question) HOT 7
- pass options to qs thru urlencoded? HOT 4
- Cannot catch SyntaxError when user provides invalid JSON in body and content-type: application/json HOT 3
- Support for content-encoding: deflate raw HOT 7
- req.body could not be accessed when send as form data, but works fine with JSON HOT 3
- Pass custom parameters to `qs` HOT 3
- Update iconv-lite to latest 0.6.3 HOT 7
- support for ndjson
- Regular Expression Denial of Service (ReDoS) in [email protected] HOT 3
- Update `debug` dependency (memory leak leading to vulnerability) HOT 1
- api calls made with invalid json HOT 5
- Add support for removing body parser limit HOT 8
- BadRequestError: request size did not match content length HOT 7
- How to handle content-type mismatch? HOT 1
- CVE-2017-20165 - debug HOT 2
- Issue HOT 1
- Debug package version in body parser showing security vulnerability HOT 10
- fails silently if the json has extra whitespace HOT 2
- json middleware does not work on content types with a `+` HOT 9
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from body-parser.