req.body undefined in POST request

Visto 13.949 veces
Saltar al primer mensaje no leído

soeren

no leída,
30 oct 2010, 18:27:2630/10/10
a 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 ]

no leída,
30 oct 2010, 21:47:0630/10/10
a 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

no leída,
31 oct 2010, 3:32:4431/10/10
a 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

no leída,
1 nov 2010, 17:17:031/11/10
a Express
I've got the same problem:

http://gist.github.com/658885

Thanks,

Michael

michael

no leída,
1 nov 2010, 17:48:121/11/10
a 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 ]

no leída,
1 nov 2010, 17:53:591/11/10
a 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

no leída,
1 nov 2010, 20:22:501/11/10
a 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

no leída,
10 dic 2010, 21:54:5710/12/10
a 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

no leída,
10 dic 2010, 23:52:1810/12/10
a Express
weird man, looks totally fine, will try that when I have a chance
Responder a todos
Responder al autor
Reenviar
0 mensajes nuevos