【问题标题】:Postman x-www-form-urlencoded returns null value for a POST request even though the input field is filled即使输入字段已填充,邮递员 x-www-form-urlencoded 也会为 POST 请求返回空值
【发布时间】:2018-12-19 10:37:03
【问题描述】:

我使用了邮递员的原始选项并且它起作用了,我的输入值按预期返回。但是当我使用 x-form-urlencoded 选项时,会返回“null”。获取请求一切正常。 在下面找到邮递员横截面的屏幕截图:

Postman Snapshot

我使用以下 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);

我的毕业生的POST请求路线的sn-p如下;

// 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);
    }
});

期待答案。谢谢。

【问题讨论】:

  • 你解决了这个问题吗?
  • 是的。谢谢@SanSolo。你的解决方案奏效了。

标签: node.js postgresql postman


【解决方案1】:

您需要一个解析器来从请求正文中获取名称。现在有一个未构建的快递。 在顶部添加这两行,它将起作用:

app.use(express.json())
app.use(express.urlencoded({ extended: false }))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-30
    • 1970-01-01
    • 1970-01-01
    • 2018-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-19
    相关资源
    最近更新 更多