Routing-controllers: How to get raw request body

Created on 21 Feb 2020  路  4Comments  路  Source: typestack/routing-controllers

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?

Most helpful comment

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!

All 4 comments

+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 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!

This was super helpful, thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

posabsolute picture posabsolute  路  4Comments

OysteinAmundsen picture OysteinAmundsen  路  3Comments

humbertowoody picture humbertowoody  路  4Comments

circy picture circy  路  3Comments

d-bechtel picture d-bechtel  路  6Comments