Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1.0.0-pre2 - this.request.body undefined :( #842

Closed
picsoung opened this issue Sep 21, 2014 · 11 comments
Closed

1.0.0-pre2 - this.request.body undefined :( #842

picsoung opened this issue Sep 21, 2014 · 11 comments
Assignees
Milestone

Comments

@picsoung
Copy link

Hi,

Me again, sorry.

I am trying to put together an API on the server, where you could POST JSON object. Used to work fine, but now I can't have access to this.request.body it's undefined.

Here is my code

Router.map(function () {
  this.route('createTeam', {
    path: "/api/team/new",
    where: "server",
    action: function(){
            if (this.request.method == 'POST') {
              console.log(this.request.body)
            }
       }
  }
}

If that comes from iron-router you might be already aware of this.
Maybe I should stick with the latest stable version instead of breaking things 😄

Thanks

@boustanihani
Copy link

Same issue here, even when using the new syntax: http://eventedmind.github.io/iron-router/#server-routing

@cmather
Copy link
Contributor

cmather commented Oct 4, 2014

It's because node deals with the http body as a stream which is confusing. You can use the bodyParser to solve this problem. On the server:

Router.use(Router.bodyParser.json())

For example. Don't forget to set the content-type header in the request. Ie content-type: 'application/json'

On Sep 20, 2014, at 5:31 PM, Nicolas Grenié notifications@github.com wrote:

Hi,

Me again, sorry.

I am trying to put together an API on the server, where you could POST JSON object. Used to work fine, but now I can't have access to this.request.body it's undefined.

Here is my code

Router.map(function () {
this.route('createTeam', {
path: "/api/team/new",
where: "server",
action: function(){
if (this.request.method == 'POST') {
console.log(this.request.body)
}
}
}
}
If that comes from iron-router you might be already aware of this.
Maybe I should stick with the latest stable version instead of breaking things

Thanks


Reply to this email directly or view it on GitHub.

@boustanihani
Copy link

but our code used to work fine with iron-router 0.9.x ??

@cmather
Copy link
Contributor

cmather commented Oct 4, 2014

I might have added that connect middleware automatically in the last version. I need to check. Perhaps it's worth adding it automatically again. Google search for examples of bodyParser for more details - it's connect middleware. I attached it to Router so you don't have to worry about including that npm package in your projects.

On Oct 4, 2014, at 7:27 AM, boustanihani notifications@github.com wrote:

but our code used to work fine with iron-router 0.9.x ??


Reply to this email directly or view it on GitHub.

@boustanihani
Copy link

@cmather I agree, adding it automatically would be great :)

@boustanihani
Copy link

Will the old behavior come back with the coming release ?

@boustanihani
Copy link

Router.use(Router.bodyParser());

causes the following output:

body-parser deprecated bodyParser: use individual json/urlencoded middlewares...

@tmeasday
Copy link
Contributor

See also #909

@tmeasday
Copy link
Contributor

@cmather - I'm guessing you are not planning on doing this automatically? Close if so.

@boustanihani - you needed to add .json() at the end -- see #842 (comment)

@cmather
Copy link
Contributor

cmather commented Oct 26, 2014

In the next release json parsing is done automatically. If you need a custom body parser you'll need to add it yourself. See the example in global_router.js

@cmather cmather closed this as completed Oct 26, 2014
@boustanihani
Copy link

Suggestion: Why not also add urlencoded & text automatically ??

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

Router.onBeforeAction(Iron.Router.bodyParser.text());

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants