Routing-controllers: @Body() empty after Post request

Created on 18 Jul 2018  路  4Comments  路  Source: typestack/routing-controllers

Hi there,
I can't retrieve data into my @Body() after a post request.

Ionic Call
processBraintreeSale(data) { console.log('processBraintreeSale CALL'); console.log(data); return this.authHttp.post(this.configProvider.getApiServer()+'/payment/braintree/sale',data, {'withCredentials': true}) .catch(this.handleError); }

Data is:
let data = {
'nonceInput' : payload.nonce,
'forfait' : forfait
}

Express server side:
````
@Post("/sale")
sale( @Body() body: any, @Session() session: Express.Session, @Res() response: Response, @Req() request: Request) {
console.log(body);
console.log(request.body);

        var nonceFromTheClient = body.nonceInput;
        var forfait = body.forfait;

        const braintreeService = Container.get(BraintreeService);
        return braintreeService.sale(nonceFromTheClient,forfait.tarifforfait); 
    }

````
body is {}
request.body is {}

Most helpful comment

If, like me, you just have this problem on a specific method, and don't want to convert the whole controller to JSON, you can do:

// should be installed as part of routing-controllers:
import { json } from 'body-parser'
import { Body, UseBefore, Post } from 'routing-controllers'

// ...

@Get('/foo')
@UseBefore(json())
doGet(@Body() body) {
    body.bar
}

All 4 comments

Here is my cors config:
routing_controllers.useExpressServer(expressApp, { cors : { origin: [ "http://localhost:8100", "http://staging.ionic."+process.env.HOSTNAME ], methods:"GET,HEAD,PUT,PATCH,POST,DELETE", allowedHeaders:"Origin,X-Requested-With,Content-Type,Accept, Accept-Encoding,Accept-Language,Authorization,Content-Length,Host,Referer,User-Agent", exposedHeaders:"Origin,X-Requested-With,Content-Type,Accept, Accept-Encoding,Accept-Language,Authorization,Content-Length,Host,Referer,User-Agent", credentials:true, optionsSuccessStatus: 200 // some legacy browsers (IE11, various SmartTVs) choke on 204 }, defaultErrorHandler: false, // disable default error handler, only if you have your own error handler // now import all our controllers. alternatively you can specify controllerDirs in routing-controller options controllers: [__dirname + "/dist/modules/**/controllers/*.js"], //middlewares: [__dirname + "/dist/middlewares/*.js"], //interceptors: [__dirname + "/dist/interceptors/*.js"] });

I found error.

I replaced
@Controller("/payment/braintree")
by
@JsonController("/payment/braintree")

If, like me, you just have this problem on a specific method, and don't want to convert the whole controller to JSON, you can do:

// should be installed as part of routing-controllers:
import { json } from 'body-parser'
import { Body, UseBefore, Post } from 'routing-controllers'

// ...

@Get('/foo')
@UseBefore(json())
doGet(@Body() body) {
    body.bar
}

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

codedoge picture codedoge  路  6Comments

szabobar picture szabobar  路  4Comments

AnthoniG picture AnthoniG  路  4Comments

iangregsondev picture iangregsondev  路  3Comments

dcbartlett picture dcbartlett  路  3Comments