Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parsers set body even when they should not parse #269

Closed
bricka opened this issue Sep 27, 2017 · 3 comments
Closed

Parsers set body even when they should not parse #269

bricka opened this issue Sep 27, 2017 · 3 comments

Comments

@bricka
Copy link

bricka commented Sep 27, 2017

Hello! I just ran into a bug in another piece of software based on an interesting behavior in body-parser:
apigee-127/swagger-tools#533

The problem here is that even if a body parser should not parse (e.g. because the content type does not match), the parser still sets req.body = {}. This led another tool to think that parsing had already occurred, because req.body was defined.

I was wondering if it is possible to not modify the request at all if parsing should not be performed. That is the behavior I would expect.

You can see an example of this behavior in the raw parser here:

req.body = req.body || {}
// skip requests without bodies
if (!typeis.hasBody(req)) {
debug('skip empty body')
next()
return
}
debug('content-type %j', req.headers['content-type'])
// determine if request should be parsed
if (!shouldParse(req)) {
debug('skip parsing')
next()
return
}

Thank you!

@dougwilson
Copy link
Contributor

dougwilson commented Sep 27, 2017

This is how 2.0 will work; you can test it out at #66

@dougwilson
Copy link
Contributor

The current behavior is documented at the top of the readme as well:

All middlewares will populate the req.body property with the parsed body when the Content-Type request header matches the type option, or an empty object ({}) if there was no body to parse, the Content-Type was not matched, or an error occurred.

@dougwilson
Copy link
Contributor

Closing since I haven't heard anything further.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants