I'm adding a Stripe webhook and I need to get raw request body, like:
app.post('/webhook', bodyParser.raw({type: 'application/json'}), (request, response) => {}
https://stripe.com/docs/webhooks/signatures#verify-official-libraries
How I can get it using routing-controller?
+1
@kkorus Did you end up finding a way around?
Spent all afternoon trying to figure this out too, but I have a solution:
Add the following middleware:
// RawBodyMiddleware.ts
import { Request, Response } from 'express';
const RawBodyMiddleware = (req: Request, res: Response, next: () => void) => {
const body = []
req.on('data', chunk => {
body.push(chunk)
})
req.on('end', () => {
const rawBody = Buffer.concat(body)
req['rawBody'] = rawBody
switch (req.header('content-type')) {
case 'application/json':
req.body = JSON.parse(rawBody.toString())
break
// add more body parsing if needs be
default:
}
next()
})
req.on('error', () => {
res.sendStatus(400)
})
}
export default RawBodyMiddleware
In your controller, add the middleware to the endpoint that requires a raw body and access the raw body property from the request using the @Req() decorator:
import { Request, Response } from 'express';
import { Req, UseBefore } from 'routing-controllers';
import RawBodyMiddleware from '../middlewares/RawBodyMiddleware'
@Post('/myendpoint')
@UseBefore(RawBodyMiddleware)
public myEndpoint(@Req() request: Request) {
const rawBody = request.rawBody
const asString = rawBody.toString()
const jsonBody = request.body
}
Hope that helps!
Spent all afternoon trying to figure this out too, but I have a solution:
Add the following middleware:
// RawBodyMiddleware.ts import { Request, Response } from 'express'; const RawBodyMiddleware = (req: Request, res: Response, next: () => void) => { const body = [] req.on('data', chunk => { body.push(chunk) }) req.on('end', () => { const rawBody = Buffer.concat(body) req['rawBody'] = rawBody switch (req.header('content-type')) { case 'application/json': req.body = JSON.parse(rawBody.toString()) break // add more body parsing if needs be default: } next() }) req.on('error', () => { res.sendStatus(400) }) } export default RawBodyMiddlewareIn your controller, add the middleware to the endpoint that requires a raw body and access the raw body property from the request using the
@Req()decorator:import { Request, Response } from 'express'; import { Req, UseBefore } from 'routing-controllers'; import RawBodyMiddleware from '../middlewares/RawBodyMiddleware' @Post('/myendpoint') @UseBefore(RawBodyMiddleware) public myEndpoint(@Req() request: Request) { const rawBody = request.rawBody const asString = rawBody.toString() const jsonBody = request.body }Hope that helps!
This was super helpful, thanks!
Most helpful comment
Spent all afternoon trying to figure this out too, but I have a solution:
Add the following middleware:
In your controller, add the middleware to the endpoint that requires a raw body and access the raw body property from the request using the
@Req()decorator:Hope that helps!