getting 'undefined' on req.param on a post

2,931 views
Skip to first unread message

Jeffrey 'jf' Lim

unread,
Jul 23, 2010, 2:02:03 AM7/23/10
to expre...@googlegroups.com
hi guys, I've got a basic simple app up, and I'm trying to access a
post param with the following code:

==========
var express = require('express'),
connect = require('connect');

var app = express.createServer(
);

app.configure(function() {
app.use(connect.methodOverride());
app.use(connect.bodyDecoder());
});

app.configure('development', function() {
app.use(connect.errorHandler({ dumpExceptions: true,
showStack: true }));
});

app.configure('production', function() {
app.use(connect.errorHandler());
});

app.get('/', function(req, res) {
res.send('hello! your params a and b = ' + req.param('a') + ',
' + req.param('b') + '\n');
});
app.post('/', function(req, res) {
res.send('hello! your params a and b = ' + req.param('a') + ',
' + req.param('b') + '\n');
});

app.listen(3000);
==============

Using curl to post always gives a 'hello! your params a and b =
undefined, undefined', whereas using it to 'get' always works. I have
done a dump, and the data is being sent correctly by curl (so no
problem there. Using curl to send to a sinatra app also works). I
believe bodyDecoder() should work for post params as well
(http://expressjs.com/guide.html#req-param-name-,
http://extjs.github.com/Connect/bodyDecoder.html), so... what gives?

-jf

--
"Every nonfree program has a lord, a master --
and if you use the program, he is your master."
    --Richard Stallman

"It's so hard to write a graphics driver that open-sourcing it would not help."
    -- Andrew Fear, Software Product Manager, NVIDIA Corporation
http://kerneltrap.org/node/7228

Jeffrey 'jf' Lim

unread,
Jul 23, 2010, 2:06:39 AM7/23/10
to expre...@googlegroups.com
ok, I guess the problem is connect's bodyDecoder. Instead of decoding
the body when it sees application/x-www-form-urlencoded, it's decoding
the query string instead?

-jf

Jeffrey 'jf' Lim

unread,
Jul 23, 2010, 2:34:54 AM7/23/10
to expre...@googlegroups.com
could somebody help me out here? Am I reading this right that it's
connect's problem?

First of all, proof that there's a problem:

$ curl 'localhost:3000/' -d 'a=postA&b=5'


hello! your params a and b = undefined, undefined

$ curl 'localhost:3000/?a=QUERYSTRING_A' -d 'a=POST_A&b=5'
hello! your params a and b = QUERYSTRING_A, undefined

selected lines from bodyDecoder.js:
=========
var queryString = require('querystring');

...

exports.decode = {
'application/x-www-form-urlencoded': queryString.parse,
'application/json': JSON.parse
};

...

// (this is the part where i get lost. Not that familiar with node.js yet!)

module.exports = function bodyDecoder(){
return function bodyDecoder(req, res, next) {
var decoder = exports.decode[mime(req)];
if (decoder) {
var data = '';
req.setEncoding('utf8');
req.addListener('data', function(chunk) { data += chunk; });
req.addListener('end', function() {
req.rawBody = data;
try {
req.body = data
? decoder(data)
: {};
} catch (err) {
return next(err);
}
next();
});
} else {
next();
}
}
};

=========

-jf

--
"Every nonfree program has a lord, a master --
and if you use the program, he is your master."
    --Richard Stallman

"It's so hard to write a graphics driver that open-sourcing it would not help."
    -- Andrew Fear, Software Product Manager, NVIDIA Corporation
http://kerneltrap.org/node/7228

Jeffrey 'jf' Lim

unread,
Jul 23, 2010, 3:09:37 AM7/23/10
to expre...@googlegroups.com
ok, I just found http://github.com/senchalabs/connect/issues/issue/59.
I guess the problem hasn't been fixed yet (the fix for
http://github.com/visionmedia/express/issues/issue/345 is in the code
I'm running).

If I were to declare app as

var app = express.createServer(
connect.bodyDecoder()
);

, my problem is solved, and I can retrieve the post params...

Whereas if i were to do

var app = express.createServer(
);
app.configure(function() {

app.use(connect.bodyDecoder());
});

this would NOT work.

express -v = '1.0.0beta',
connect exports.version = '0.2.2'

vision media [ Tj Holowaychuk ]

unread,
Jul 23, 2010, 10:16:04 AM7/23/10
to expre...@googlegroups.com
If your not using HEAD right now some things might not work, that
was a pretty bad bug. The reason that we parse with queryString.parse is
because its a urlencoded string just like any other, however multipart/form-data 
is a different beast and connect-form can help you there

--
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
Reply all
Reply to author
Forward
0 new messages