Meteor Iron Router How to get POST data
Asked Answered
C

1

6

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.

Cask answered 4/11, 2014 at 19:23 Comment(3)
possible duplicate of How do I access Request Parameters in Meteor?Fascia
@ChristianFritz it's not a duplicate. The code here is totally fine and the problem is on iron-router side.Higa
For anyone arriving from Google who wants to use the above code with arrow functions, you don't need to get request and response from this. They are also passed to the callback. The first line can be rewritten as Router.route('/api/gatewaysusers', (request, response) => {Boyla
H
8

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.

Higa answered 5/11, 2014 at 8:15 Comment(4)
This is working, Thank you. Now i can get the body using this.request.body.Cask
It seems to be fixed by the way, this.request.body now has all the data expected without this extra call.Santos
@Santos Do you know precisely in which version of IR it was solved?Higa
@apendua sorry, don't know it for sure. We used iron:[email protected] in our recent project.Santos

© 2022 - 2024 — McMap. All rights reserved.