req.body undefined in POST request

14.016 Aufrufe
Direkt zur ersten ungelesenen Nachricht

soeren

ungelesen,
30.10.2010, 18:27:2630.10.10
an 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 ]

ungelesen,
30.10.2010, 21:47:0630.10.10
an 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

ungelesen,
31.10.2010, 03:32:4431.10.10
an 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

ungelesen,
01.11.2010, 17:17:0301.11.10
an Express
I've got the same problem:

http://gist.github.com/658885

Thanks,

Michael

michael

ungelesen,
01.11.2010, 17:48:1201.11.10
an 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 ]

ungelesen,
01.11.2010, 17:53:5901.11.10
an 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

ungelesen,
01.11.2010, 20:22:5001.11.10
an 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

ungelesen,
10.12.2010, 21:54:5710.12.10
an 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

ungelesen,
10.12.2010, 23:52:1810.12.10
an Express
weird man, looks totally fine, will try that when I have a chance
Allen antworten
Antwort an Autor
Weiterleiten
0 neue Nachrichten