req.body undefined in POST request

13,787 views
Skip to first unread message

soeren

unread,
Oct 30, 2010, 6:27:26 PM10/30/10
to Express
Hi everyone,

I seem to have a problem with req.body being "undefined" when using a
POST request. Im using expresso to do the POST:

-- snip
assert.response(server, {
url: '/',
method: 'POST',
data: '{"somevar": "somestring"}'
}, {
body: 'someresponse'
});
-- /snip

express = require('express').
server = express.createServer();

server.configure(function() { server.use(express.bodyDecoder()); };

server.post('/', function(req, res) {
console.log(req.body);
});

I'm using version 1.0.0rc4.
Any pointers would be greatly appreciated.

vision media [ Tj Holowaychuk ]

unread,
Oct 30, 2010, 9:47:06 PM10/30/10
to expre...@googlegroups.com
few pointers:

  1.  you are not required to use app.configure() if you dont plan on (or need) different environments
  2. you need to set the Content-Type to either application/json or application/x-www-form-urlencoded, these are the two that bodyDecoder parses


--
You received this message because you are subscribed to the Google Groups "Express" group.
To post to this group, send email to expre...@googlegroups.com.
To unsubscribe from this group, send email to express-js+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/express-js?hl=en.




--
Tj Holowaychuk
Vision Media
President & Creative Lead

soeren

unread,
Oct 31, 2010, 3:32:44 AM10/31/10
to Express
Aah ok, makes sense and works, thanks a lot.

On Oct 31, 2:47 am, "vision media [ Tj Holowaychuk ]" <t...@vision-
media.ca> wrote:
> few pointers:
>
>    1.  you are not required to use app.configure() if you dont plan on (or
>    need) different environments
>    2. you need to set the Content-Type to either application/json or
>    application/x-www-form-urlencoded, these are the two that bodyDecoder parses
>
> > express-js+...@googlegroups.com<express-js%2Bunsubscribe@googlegrou ps.com>
> > .

michael

unread,
Nov 1, 2010, 5:17:03 PM11/1/10
to Express
I've got the same problem:

http://gist.github.com/658885

Thanks,

Michael

michael

unread,
Nov 1, 2010, 5:48:12 PM11/1/10
to Express
Worked, when I put the app.use calls in front.

However for me, nested params are not parsed. For the user example
(taken from the Express.js documentation) I get:

{ 'user[name]': 'adsf', 'user[email]': 'dsfa' }

Thus, req.body.user doesn't work. Is this intended?

-- Michael

vision media [ Tj Holowaychuk ]

unread,
Nov 1, 2010, 5:53:59 PM11/1/10
to expre...@googlegroups.com
if you are using node 0.3.x then that is what you will get, until I start supporting the 0.3.x branch

To unsubscribe from this group, send email to express-js+...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/express-js?hl=en.

michael

unread,
Nov 1, 2010, 8:22:50 PM11/1/10
to Express
I think I'll go with application/json for now. Thanks a lot!

On Nov 1, 10:53 pm, "vision media [ Tj Holowaychuk ]" <t...@vision-

Luke Galea

unread,
Dec 10, 2010, 9:54:57 PM12/10/10
to Express
Hi all,

I'm running into this issue. I am using application/json, but req.body
== undefined. Node version is v0.2.5, Expresso is 0.70, Express is
1.0.

Express:
app.configure(function(){
...
app.use(express.bodyDecoder());
...
app.post('/observations', function(req, res) {
console.log(req.body);
...

Expresso:
'POST /observations': function(){
assert.response(app,
{ url: '/observations', method: 'POST',
'Content-Type': 'application/json',
data: JSON.stringify({ observation: { time_label: 'something
' } })
},
{ status: 200, headers: { 'Content-Type': 'text/html;
charset=utf-8' }},
function(res){
assert.includes(res.body, 'show-observation-page');
});
}

Any ideas? Thanks in advance!
> > > > > > > POST request. Im usingexpressoto do the POST:

tjholowaychuk

unread,
Dec 10, 2010, 11:52:18 PM12/10/10
to Express
weird man, looks totally fine, will try that when I have a chance
Reply all
Reply to author
Forward
0 new messages