Routing-controllers: Support for body-parser urlencoded, out of the box?

Created on 3 Aug 2017  路  3Comments  路  Source: typestack/routing-controllers

Hi,

I have an endpoint and I am calling into it and passing in x-www-form-urlencoded values from postman.

It arrives in the controller but the controller Body property is empty.

   @Body() transaction

It seems that routing-controllers uses Body-Parser out of the box as it parses JSON right ?

But urlencoded, do I need to configure something? Or pass something into @Body ?

Does anyone have an example ?

Appreciate any insight anyone has.

Thanks

question

All 3 comments

Currently is not supported out of the box:
https://github.com/pleerock/routing-controllers/blob/master/src/driver/express/ExpressDriver.ts#L90

The easiest way is to create a parser object on app bootstrap:

import * as bodyParser from "body-parser";

// create application/x-www-form-urlencoded parser
export const urlencodedParser = bodyParser.urlencoded({ extended: false })

And then use it in your controller as a middleware mounted before the route:

@JsonController("/urlform")
class UrlFormController {

    @Get("/test")
    hello() {
        return "Hello World!";
    }

    @Post("/send")
    @UseBefore(urlencodedParser)
    handleForm(@Body() form: BodyFormType) {
        // your form should be here
    }

}

Or on the whole controllers endpoint if you have multiple forms:

@JsonController("/urlform")
@UseBefore(urlencodedParser)
class UrlFormController {
    // ...
}

Thanks! Just what I needed, actually I had it added as a global middleware but this is way better as I don't want my other endpoints to support this method.

Thanks!

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

circy picture circy  路  3Comments

hellraisercenobit picture hellraisercenobit  路  4Comments

mlarsson picture mlarsson  路  5Comments

szabobar picture szabobar  路  4Comments

GBeushausen picture GBeushausen  路  5Comments