typestack / routing-controllers

Create structured, declarative and beautifully organized class-based controllers with heavy decorators usage in Express / Koa using TypeScript and Routing Controllers Framework.
MIT License
4.41k stars 394 forks source link

@Body() empty after Post request #423

Closed davidquintard closed 6 years ago

davidquintard commented 6 years ago

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 {}

davidquintard commented 6 years ago

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"]
});
davidquintard commented 6 years ago

I found error.

I replaced

@Controller("/payment/braintree")

by

@JsonController("/payment/braintree")
NaridaL commented 5 years ago

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
}
github-actions[bot] commented 3 years ago

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.