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

set req.length #214

Open
dcolens opened this issue Dec 20, 2016 · 13 comments
Open

set req.length #214

dcolens opened this issue Dec 20, 2016 · 13 comments
Assignees
Labels

Comments

@dcolens
Copy link

dcolens commented Dec 20, 2016

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)

@dougwilson
Copy link
Contributor

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 dougwilson self-assigned this Dec 21, 2016
@dcolens
Copy link
Author

dcolens commented Dec 22, 2016

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 :)

@jordonias
Copy link

Can you just use the verify option?

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

@dougwilson
Copy link
Contributor

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.

@dcolens
Copy link
Author

dcolens commented Feb 27, 2017

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.

@dougwilson
Copy link
Contributor

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

@dcolens
Copy link
Author

dcolens commented Feb 27, 2017

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

@dougwilson
Copy link
Contributor

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

@dcolens
Copy link
Author

dcolens commented Feb 27, 2017

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.

@dougwilson
Copy link
Contributor

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?

@dcolens
Copy link
Author

dcolens commented Feb 28, 2017

yes pretty much

@rmullenix
Copy link

Any update on this?

@pranshu667
Copy link

can i take up this issue ?

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

5 participants