0

All my paths are accepting JSON only. So it makes sense to use bodyParser.json() as middleware. The problem is that the authentication process is based on the raw payload. Authentication is done via a custom middleware. Is there any way to use bodyParser.json() and still being able to access the raw request payload?

I have found this solution

app.use(bodyParser.json({verify:function(req,res,buf){req.rawBody=buf}}))

here https://stackoverflow.com/a/30196898/3531412 but I think this is not the way verify is meant to be used.

Any suggestion here?

schlicki
  • 374
  • 4
  • 14
  • 1
    Why don't you call your auth middleware before calling the bodyParser ? – BENARD Patrick Mar 27 '20 at 12:46
  • As @BENARDPatrick said if you define the app.use(AuthMiddleware) before the app.use(bodyParser). the authentication middleware will be called before the parsing middleware, and the request body would be raw – DavidCohen Mar 27 '20 at 12:58
  • I think I can't do that because I have different authentication methods for some routes. But maybe I can at least add the raw body to all requests through a Middleware. I will try that. Thanks for the hints! – schlicki Mar 28 '20 at 13:21

0 Answers0