[英]Unable to POST values to Express in Node.js
I'm learning to use Express (v4) in Node.js. 我正在学习在Node.js中使用Express(v4)。 I'm building a basic REST api. 我正在构建一个基本的REST API。 My api has one endpoint: /orders. 我的api有一个端点:/ 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? 我想念什么?
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. IIRC Postman当前未明确发送带有表单的Content-Type,因此您需要确保为您的请求手动设置Content-Type: application/x-www-form-urlencoded
。
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. 还要确保在堆栈中的那些路由之前,已加载加载了body-parser
或其他一些能够解析application/x-www-form-urlencoded
中间件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.