【问题标题】:POST request in Postman doesn't work when adding query params添加查询参数时,Postman 中的 POST 请求不起作用
【发布时间】:2019-10-19 10:21:46
【问题描述】:

我有一个 Express 路由,可以将用户个人资料发布到数据库中:

router.post(
  "/:account_id",
  [
    auth,
    [
      check("name", "Please enter your name")
        .not()
        .isEmpty(),
      check("orgName", "Please enter your organization name")
        .not()
        .isEmpty()
    ]
  ],
  async (req, res) => {
    const errors = validationResult(req);
    if (!errors.isEmpty()) {
      return res.status(400).json({ errors: errors.array() });
    }
    try {
      const accountId = req.params.account_id;

      const { name, orgName } = req.body;

      // Check for the existing profile
      let query = await db.query(
        `SELECT 1 FROM profile WHERE profile.account_id='${accountId}'`
      );

      if (query.rowCount !== 0) {
        return res.status(400).send({
          errors: [
            {
              msg: "You have already created your profile"
            }
          ]
        });
      }

      // Insert the profile
      query = await db.query(
        `INSERT INTO profile (
        account_id, profile_name, company_name) VALUES ('${accountId}', '${name}', '${orgName}') returning profile_id, account_id, profile_name, company_name`
      );

      const profile = query.rows[0];

      return res.json(profile);
    } catch (err) {
      console.error(err.message);
      res.status(500).send("Server error");
    }
  }
);

此请求不起作用,并在 Postman 中使用 HTML 代码给出 CANNOT POST 错误。但是,当我从 URL 中删除 :account_id 参数并手动写入即 50 时,请求有效。这里的查询参数有什么问题?

标题: x-auth-token:授权令牌 内容类型:application/x-www-form-urlencoded

UPD:我得到的错误:

【问题讨论】:

  • 您在 Postman 中的配置如何?什么是服务器响应(主要是状态码、正文)?
  • 如果有帮助,我已附上屏幕截图。

标签: postgresql express postman


【解决方案1】:

问题是您的后端控制器需要 path 中的 account_id 参数,而您在 查询字符串 中提供了它。要使其工作,请在 Postman 中将 URL 的结尾更改为 api/profile/:account_id,删除查询字符串。此外,此页面显示如何在 Postman 上设置 URL 参数:https://learning.getpostman.com/docs/postman/sending_api_requests/requests/

【讨论】:

    猜你喜欢
    • 2021-10-05
    • 1970-01-01
    • 2021-12-24
    • 2017-08-23
    • 2021-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多