2

I'm going to test via curl a simple application with Nodejs.

curl command is:

curl -i -X POST -H 'Content-Type: application/json' -d '{"name": "Name"}' http://localhost:3000/users

and Node.js runs like:

app.post('/users', users.addUser);

and somewhere else in users:

exports.addUser = function(req, res) {
console.log(req.query);
//etc.

output is {}. req.body, instead, is undefined.

How can I retrieve that parameters with curl? Am I missing something?

Fra H
  • 335
  • 5
  • 20

1 Answers1

2

To read POST data with Express, you will have to use app.use(express.bodyParser());.

Quoting here:

This property is an object containing the parsed request body. This feature is provided by the bodyParser() middleware, though other body parsing middleware may follow this convention as well. This property defaults to {} when bodyParser() is used.

Although it says Express defaults to {} when bodyParser() is used, I think that's a typographical error and is the opposite.

hexacyanide
  • 76,426
  • 29
  • 148
  • 154