How to get the raw POST/PUT payload in nodejs express

I was getting lots of errors in my Express-based REST API due to invalid Json payloads being posted to my endpoint.
Express just throws an exception, but it doesn’t give you the raw payload to see what is causing the error.
I dug into the source code of the body parser and it revealed this simple trick to get the raw payload in req.rawBody:

<br /> app.use(express.bodyParser({<br /> verify: function(req, res, buf, encoding) {<br /> req.rawBody = buf.toString(encoding);<br /> }<br /> }));<br />

Related Posts