简体   繁体   English

邮递员x-www-form-urlencoded会为POST请求返回空值,即使输入字段已填写

[英]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: 在邮递员横截面的屏幕截图下方找到:

Postman Snapshot 邮递员快照

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.

相关问题 包含ascii字符的POST请求[x-www-form-urlencoded] - POST request containing ascii characters [x-www-form-urlencoded] 如何使发布请求具有bas ic auth和内容类型x-www-form-urlencoded - how to make post request baswith ic auth and content type x-www-form-urlencoded 如何使用“内容类型”:“ application / x-www-form-urlencoded”发出发布请求? - How to make a Post request with “content type”: “application/x-www-form-urlencoded”? 具有 application/x-www-form-urlencoded 格式的 Node.js Axios POST 请求? - Node.js Axios POST request with application/x-www-form-urlencoded format? 使用application / x-www-form-urlencoded使用node.js在发布请求中发送数组 - Send Array in post request using node.js using application/x-www-form-urlencoded node-fetch 发送 post 请求,正文为 x-www-form-urlencoded - node-fetch send post request with body as x-www-form-urlencoded 使用 axios 发出 x-www-form-urlencoded 请求 - Making a x-www-form-urlencoded request with axios 节点js如何识别邮递员扩展客户端请求来自“表单数据”或“ x-www-form-urlencoded” - node js how to identify postman extention client request came from “form-data” or “x-www-form-urlencoded” NodeJS从formData x-www-form-urlencoded获取值 - NodeJS get value from formData x-www-form-urlencoded 使用“Content-Type”:“application/x-www-form-urlencoded”从 axios 发送帖子请求会给出 401 Unauthorized 响应 - Sending post request from axios with "Content-Type" : "application/x-www-form-urlencoded" gives an 401 Unauthorized response
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM