Reputation: 23737
I have an express app and a POST route:
app.post('/test', function(req, res){
//res.send(req.body.title + req.body.body)
console.log(req.params);
console.log(req.body);
console.log(req.body.user);
console.log(req.body.feedback);
console.log("ok");
//return;
});
I try to do ~ $ curl -X POST "http://xxxx.herokuapp.com/test?user=hello"
and I get:
TypeError: Cannot read property 'user' of undefined
at Router.<anonymous> (/app/app.js:40:22)
at done (/app/node_modules/express/lib/router/index.js:250:22)
at middleware (/app/node_modules/express/lib/router/index.js:244:9)
at param (/app/node_modules/express/lib/router/index.js:227:11)
at pass (/app/node_modules/express/lib/router/index.js:232:6)
at Router._dispatch (/app/node_modules/express/lib/router/index.js:255:4)
at Object.handle (/app/node_modules/express/lib/router/index.js:45:10)
at next (/app/node_modules/express/node_modules/connect/lib/http.js:198:15)
at Object.methodOverride [as handle] (/app/node_modules/express/node_modules/connect/lib/middleware/methodOverride.js:35:5)
at next (/app/node_modules/express/node_modules/connect/lib/http.js:198:15)~ $
Shouldn't that POST work?
Thanks
Upvotes: 3
Views: 4904
Reputation: 14602
The selected answer is wrong. It's very clear you wanted something from the request body (given that you say POST), not the query string. You need to make sure the body parser is in your middleware stack:
app.use(express.bodyParser());
Once you do this, you can use req.body
.
(Furthermore, as others have mentioned, your curl is wrong as well. You're putting something in the query string there instead of the request body.)
Upvotes: 4
Reputation: 189
No, the curl invocation is wrong. Try
curl -X POST "http://xxxx.herokuapp.com/test" -d user=hello
Upvotes: 1
Reputation: 13332
Nope that won't work, because you are adding the args to the URL (thats what you would do for a GET). For a post you need to:
curl -d "user=hello" http://xxxx.herokuapp.com/test
Upvotes: 1
Reputation: 105220
No, the parameter you are passing is not on the post body but in the querystring.
According to the docs, to access it you must do:
req.query.user
Upvotes: 4