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

Access raw body and json body #43

Open
gerwinbrunner opened this issue Aug 17, 2016 · 2 comments
Open

Access raw body and json body #43

gerwinbrunner opened this issue Aug 17, 2016 · 2 comments

Comments

@gerwinbrunner
Copy link

Hi,

how can I get the raw body and also the json body from a request?
Currently I use

Picker.middleware( bodyParser.json() );

And this works fine, but for security checks I would also need the raw body. How could I get both of them?

Best,
Gerwin

@karlpokus
Copy link

@gerwinbrunner Re: raw body. Have you tried just passing a function to Picker.middleware that listens for 'data'?

@dgreuel
Copy link

dgreuel commented Apr 12, 2018

@gerwinbrunner I know this is coming really late, but I figured out how to do this in case it helps you or anyone else. From the body-parser documentation:

verify
The verify option, if supplied, is called as verify(req, res, buf, encoding), where buf is a Buffer of the raw request body and encoding is the encoding of the request. The parsing can be aborted by throwing an error.

So you can pass a verify function to bodyParser.json like this:
Picker.middleware(bodyParser.json({ verify: verifyParsing }));

and then in that function, grab out the raw body:

let rawBody;
const verifyParsing = (req, res, buf, encoding) => {
  rawBody = buf;
}

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

No branches or pull requests

3 participants