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

textParser returns an object when body is empty #44

Closed
hans-d opened this issue Sep 5, 2014 · 8 comments
Closed

textParser returns an object when body is empty #44

hans-d opened this issue Sep 5, 2014 · 8 comments
Labels

Comments

@hans-d
Copy link

hans-d commented Sep 5, 2014

See https://github.com/expressjs/body-parser/blob/master/lib/types/text.js#L51

Would expect it to read req.body = req.body || '' instead of req.body = req.body || {}

With this line, req.body will return an object when no body is provided

@dougwilson
Copy link
Contributor

Yes. The reason is historical that req.body is always set to {} unless there was actually a matching body. The main reason is stacking the parsers. In the next major, req.body really shouldn't even get set unless there was a body. At least setting it to {} allows you to differentiate between no body and a body of an empty string.

@tlivings
Copy link

tlivings commented Sep 5, 2014

I'd think that in the case of bodyParser.text() you'd likely prefer the empty string to an object. The expectation is that it's text, not an object. If it gets automatically assigned to {} then !req.body will never pass, and the developer will have no idea why.

@aredridel
Copy link

Yeah, definitely should be an empty string in that case!

I'd expect undef if there was no body.

@dougwilson
Copy link
Contributor

Right. I'm not disagreeing, but I was just saying the reason it's set to {} when the body was not a text body was because of backwards-compatibility reasons. In the next major, req.body will just be undefined if there was no parsed body on the request.

@dougwilson
Copy link
Contributor

Anyway, if you post a body that is plain text and the body is empty (so Content-Length: 0), then req.body is set to an empty string ('') by this module. It's only set to {} if the request didn't actually have a text body at all.

@dougwilson
Copy link
Contributor

The test https://github.com/expressjs/body-parser/blob/1.7.0/test/text.js#L33-L42 shows that the empty body comes through as req.body === '' if you are in doubt.

@dougwilson
Copy link
Contributor

Verified the behavior is a bug within type-is and how it is interpreting the HTTP spec.

@hans-d
Copy link
Author

hans-d commented Sep 5, 2014

thanks for the help!

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

No branches or pull requests

4 participants