使用 NodeJS / Axios 和 Postman,我无法从正文中包含数组的 POST 请求中获得响应
I cant get a response from a POST request with an array in the body, Using NodeJS / Axios and Postman
这是课程测验,这是我创建 React 应用程序所需的最基本信息。但是,虽然端点 URL 是正确的,但当我尝试请求产品列表时,页面“/products”returns 出现“400”错误。我得到的指示是:
获取产品列表
- 路线:/产品
- 正文:数组
- 方法:POST
{
"product-codes": [
"98798729",
"84876871",
"29879879",
]
}
我的index.js
...
app.post(`/products`, async (req, res) => {
try {
const response = await axios.post(`${apiURL}/products`);
// console.log(response.data);
res.status(200).json(response.data);
} catch (error) {
res.status(400).json({ message: error.message });
}
});
邮递员[=36=]
我使用 http://localhost:4000/products
并传递 Body / Raw /JSON:
{
"product-codes": [
"98798729",
"84876871",
"29879879",
]
}
但是我进不去!我没有看到明显的东西,因为这是一个非常长且复杂的测验的切入点。谢谢
我从代码中看到的是一个递归的长调用。
app.post(`/products`, async (req, res) => {
try {
const response = await axios.post(`${apiURL}/products`); // calling the same end point
// console.log(response.data);
res.status(200).json(response.data);
} catch (error) {
res.status(400).json({ message: error.message });
}
});
你应该这样做:
app.post(`/products`, async (req, res) => {
// do your logic
// when you pass body from postman on this endpoint
// you will receive the body here and save it to DB
// or do the manipulation and then send back the response
res.status(200).json(req.body.data);
});
我强烈建议您先阅读一些教程,以了解 API 的工作原理以及如何 create simple API 使用 Node.js
。
这是课程测验,这是我创建 React 应用程序所需的最基本信息。但是,虽然端点 URL 是正确的,但当我尝试请求产品列表时,页面“/products”returns 出现“400”错误。我得到的指示是:
获取产品列表
- 路线:/产品
- 正文:数组
- 方法:POST
{
"product-codes": [
"98798729",
"84876871",
"29879879",
]
}
我的index.js
...
app.post(`/products`, async (req, res) => {
try {
const response = await axios.post(`${apiURL}/products`);
// console.log(response.data);
res.status(200).json(response.data);
} catch (error) {
res.status(400).json({ message: error.message });
}
});
邮递员[=36=]
我使用 http://localhost:4000/products
并传递 Body / Raw /JSON:
{
"product-codes": [
"98798729",
"84876871",
"29879879",
]
}
但是我进不去!我没有看到明显的东西,因为这是一个非常长且复杂的测验的切入点。谢谢
我从代码中看到的是一个递归的长调用。
app.post(`/products`, async (req, res) => {
try {
const response = await axios.post(`${apiURL}/products`); // calling the same end point
// console.log(response.data);
res.status(200).json(response.data);
} catch (error) {
res.status(400).json({ message: error.message });
}
});
你应该这样做:
app.post(`/products`, async (req, res) => {
// do your logic
// when you pass body from postman on this endpoint
// you will receive the body here and save it to DB
// or do the manipulation and then send back the response
res.status(200).json(req.body.data);
});
我强烈建议您先阅读一些教程,以了解 API 的工作原理以及如何 create simple API 使用 Node.js
。