body-parser icon indicating copy to clipboard operation
body-parser copied to clipboard

set req.length

Open dcolens opened this issue 9 years ago • 14 comments

it would be nice that body-parser sets the length of the body, my use case is I'd like to know the length of a JSON payload POSTED using chunked-encoding, so there is no content-length header and req.body is set to an Object thanks to body-parser.

Sounds like the only way to get the body size is to stringify req.body and get the length :/ or use raw-parser and JSON.parse.

Any chance we could set req.length or similar to the size of the raw body before parsing it ? (other suggestions are welcome)

dcolens avatar Dec 20 '16 11:12 dcolens

That sounds mostly OK, but we need to impact what setting a length property to req will have against all the existing application using this module out there. That property seems likely to be already in-use, but maybe not. Help determining one way or the other would be very awesome :) !

dougwilson avatar Dec 21 '16 17:12 dougwilson

Agreed about the name conflict, don't see a way to validate the it upfront.

What about req.bodyLength, even if it is used, it is likely for the same reason :)

dcolens avatar Dec 22 '16 07:12 dcolens

Can you just use the verify option?

bodyParser.json({
  verify: function(req, res, buf, encoding) {
    req.bodyLength = buf.length;
  }
})

jordonias avatar Dec 22 '16 20:12 jordonias

Hi @dcolens I'm circling back around on this, and was wondering if you could say what your use-case is. The reason is not to dismiss this request, but rather to determine the best way to implement the request. Basically looking to better understand the "why?" to it to make sure everything lines up once implemented.

dougwilson avatar Feb 26 '17 22:02 dougwilson

I receive a JSON body, in some cases I want to forward it to another service and I need to stringify it, but I only want to do that if its size is less than 100KB. The only way to test it, is to JSON.stringify it which is blocking (I know that JSON.parse is too), I'd rather check the size before calling JSON.stringify.

dcolens avatar Feb 27 '17 15:02 dcolens

@dcolens gotcha. What do you do if the size is over 100KB?

dougwilson avatar Feb 27 '17 15:02 dougwilson

POST it to a remote service, i.e. JSON.stringify

dcolens avatar Feb 27 '17 16:02 dcolens

Oh, I thought it said you didn't want to send it to the remove service if it was over 100KB?

dougwilson avatar Feb 27 '17 16:02 dougwilson

Sorry, misunderstood, indeed, in that case I omit the incoming payload from the outgoing call to the remote service! The idea is that some calls are tracked, but if the user sends too much payload, he does not benefit from the tracking.

dcolens avatar Feb 27 '17 16:02 dcolens

Makes sense! And to clarify, in all the cases you do actually look at the body locally, just the size is what determines if you want to forward it?

dougwilson avatar Feb 27 '17 16:02 dougwilson

yes pretty much

dcolens avatar Feb 28 '17 09:02 dcolens

Any update on this?

rmullenix avatar Jan 25 '19 02:01 rmullenix

can i take up this issue ?

pranshu667 avatar Aug 26 '21 04:08 pranshu667