You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It seems that when the body is actually null/empty (as with a GET), bodyParser.text will set {} as the body. This can be confusing to later handlers. It seems to me that it should either be left null or set to the empty string.
Background on my use case: I'm implementing a proxy that transforms the request based on the user's authorization. There may or may not be a request body, but if there is, I may need to transform it and then restream it. So my request handling looks like:
If it's a GET request and there is no actual body, bodyParser still sets one, authorizationHandler doesn't know whether there was a real body or not, and when reqJsonRestreamer runs against {} it tries to stream a request body where there was none originally, and the request hangs.
The workaround is to have the next handler check if the body is an object or string treat the object as an empty body. But it shouldn't be necessary if bodyParser just set body accurately in this case.
The text was updated successfully, but these errors were encountered:
Hi! Yes, the behavior you describe is by design, for better or for worse. The behavior has been changed, to be inline with what you expect, in the 2.0 branch (https://github.com/expressjs/body-parser/tree/2.x). You will have to wait for the 2.0 release (subscribe to PR #66 for updates), just add the following to your package.json: "body-parser": "expressjs/body-parser#2.x", or work-around this (you can use the type-is module's hasBody method to detect if there was really a body or not).
And if it helps, I 100% agree with you the way 1.x works in this regards sucks :) We had to do req.body = {} at the time long ago because other modules would look to see if req.body was falsy and then try to parse (which would hang since we read the body already) in addition to a lot of people just having that like req.body.some_property without checking if req.bdoy existed or not :(
Looking at:
body-parser/lib/types/text.js
Line 62 in 59c3615
It seems that when the body is actually null/empty (as with a GET),
bodyParser.text
will set{}
as the body. This can be confusing to later handlers. It seems to me that it should either be left null or set to the empty string.Background on my use case: I'm implementing a proxy that transforms the request based on the user's authorization. There may or may not be a request body, but if there is, I may need to transform it and then restream it. So my request handling looks like:
If it's a GET request and there is no actual body,
bodyParser
still sets one,authorizationHandler
doesn't know whether there was a real body or not, and whenreqJsonRestreamer
runs against{}
it tries to stream a request body where there was none originally, and the request hangs.The workaround is to have the next handler check if the body is an object or string treat the object as an empty body. But it shouldn't be necessary if
bodyParser
just set body accurately in this case.The text was updated successfully, but these errors were encountered: