[英]Postman x-www-form-urlencoded returns null value for a POST request even though the input field is filled
I used the postman's raw option and it worked, my input value returned as expected. 我使用了邮递员的原始选项,并且可以正常工作,我的输入值按预期返回。 But when I use the x-form-urlencoded option, "null" is returned.
但是,当我使用x-form-urlencoded选项时,将返回“ null”。 The get request all works well.
获取请求都很好。 Find below the screenshot of the postman cross-section:
在邮递员横截面的屏幕截图下方找到:
I manually created the tables and rows with the following postgres scripts: 我使用以下postgres脚本手动创建了表和行:
CREATE TABLE graduates (id SERIAL PRIMARY KEY, name TEXT);
CREATE TABLE offers (id SERIAL PRIMARY KEY, title TEXT, graduate_id INTEGER REFERENCES graduates (id) ON DELETE CASCADE);
INSERT INTO graduates (name) VALUES ('Elie'), ('Michael'), ('Matt'), ('Joel');
INSERT INTO offers (title, graduate_id) VALUES ('Teacher', 1), ('Super Teacher', 2), ('Mathematician', 3), ('Developer', 4), ('Super Doctor 1', 3), ('Super Doctor 2', 4), ('Super Developer 1', 2);
A snippet of my graduates' route for POST request is as follows; 我的毕业生申请POST路线的摘要如下:
// This route is mounted on /graduates in app.js
router.post("/", async (req, res, next) => {
try {
const result = await db.query(
"INSERT INTO graduates (name) VALUES ($1) RETURNING *",
[req.body.name]
);
return res.status(201).send(result.rows[0]);
} catch(err) {
return next(err);
}
});
I look forward to an answer. 我期待一个答案。 Thanks.
谢谢。
You need a parser to getthe name from request body. 您需要一个解析器才能从请求正文中获取名称。 Now there's one unbuilt with express.
现在有一个未构建的Express。 Add these two lines at the top and it will work:
在顶部添加这两行,它将起作用:
app.use(express.json())
app.use(express.urlencoded({ extended: false }))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.