TJR
TJR

Reputation: 6577

Meteor iron:router prepare request.body when JSON

Currently I play around with iron:router's solution for a restful API. For this I use the .put, .get ... methods which are iron:router has implemented.

This is my example I work with:

Router.route('/api', {where:'server'})
.put(function(){

  var req;
  req = this.request;
  console.log(req.body);

  this.response.end('PUT finished.');

});

When I execute the following I will get the expected response (PUT finished):

curl -XPUT "http://localhost:4000/api " -d'{"name": "timo"}'

But the console.log(req.body) returns a strange value converted to an object.

The returned value is:

{ 
  '{"name": "timo"}\n' : '' 
}

It seems that iron:router trys to convert the body into an object but did not recognized that the given request string is a valid JSON string.

Is there smth I did wrong ? I did not find anything helpful yet to prepare iron:router that the given request body is still JSON.

Maybe its a better solution not to tell iron:router that the given request is a JSON and instead to tell iron:router that it shouldn't do anything so I can convert the JSON string by myself ?

Upvotes: 1

Views: 257

Answers (1)

Christian Fritz
Christian Fritz

Reputation: 21364

You didn't specify the content type in your curl request. Try this instead:

curl -XPUT "http://localhost:4000/api " -d'{"name": "timo"}' -H "content-type: application/json"

With that it works for me:

I20150522-09:59:08.527(-7)? Request { name: 'timo' }

(and without it doesn't).

Upvotes: 1

Related Questions