set req.length
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)
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 :) !
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 :)
Can you just use the verify option?
bodyParser.json({
verify: function(req, res, buf, encoding) {
req.bodyLength = buf.length;
}
})
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.
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 gotcha. What do you do if the size is over 100KB?
POST it to a remote service, i.e. JSON.stringify
Oh, I thought it said you didn't want to send it to the remove service if it was over 100KB?
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.
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?
yes pretty much
Any update on this?
can i take up this issue ?