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:
app.use(express.bodyParser({
verify: function(req, res, buf, encoding) {
req.rawBody = buf.toString(encoding);
}
}));