javascriptnode.jsmeteoriron-router

Meteor Iron Router How to get POST data


I am trying to pass POST data to an Iron Router route from outside meteor but it doesn't work. The request body is empty.

I have tried outputting the request body to check if the data were present, but it's just empty.

Router.route('/api/gatewaysusers', function() {
        body = this.request.body;
        console.log(this.request)
        // this.response.write(body);
        this.response.end("Call served");


}, {where: 'server'})

Any idea ? Thank you.


Solution

  • The request.body is empty because iron-router lacks middleware responsible for extracting url-encoded data. This is a BUG, which will be hopefully solved in later versions. For now you can just add:

    Router.onBeforeAction(Iron.Router.bodyParser.urlencoded({
        extended: false
    }));
    

    somewhere on your server and it should work fine. Look here for more details.