0

I'm learning to use Express (v4) in Node.js. I'm building a basic REST api. My api has one endpoint: /orders. I want to be able to get a list of orders and POST new orders. To do this, I've defined the following routes:

app.get('/orders', myApi.getOrders);
app.post('/orders', myApi.createOrder);

I can successfully call these routes via Postman. However, when I attempt to POST values to create an order, my data isn't there. I've noticed that I can read the data the request headers values. However, I can't read any of the key/values set in the form-data. I'm trying to read my request using the following:

createOrder: function(req, res) {
  try {
    console.log(req);
  } catch (ex) {
    console.log(ex);
  }
};

Nothing fancy. I don't understand why the headers are there. However, the form-data isn't. What am I missing?

user3284007
  • 1,697
  • 7
  • 27
  • 43

1 Answers1

1

IIRC Postman currently does not explicitly send a Content-Type with forms, so you need to make sure to manually set Content-Type: application/x-www-form-urlencoded for your request.

Also make sure you have the body-parser or some other middleware capable of parsing application/x-www-form-urlencoded loaded before those routes in your stack.

mscdex
  • 104,356
  • 15
  • 192
  • 153