首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >添加查询参数时,Postman中的POST请求不起作用

添加查询参数时,Postman中的POST请求不起作用
EN

Stack Overflow用户
提问于 2019-06-04 05:06:09
回答 1查看 1.9K关注 0票数 0

我有一个将用户配置文件发布到数据库中的Express路由:

代码语言:javascript
复制
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中给出CANNOT POST错误和HTML代码。但是,当我从URL中删除:account_id参数并手动写入50时,请求就可以工作了。这里的查询参数有什么问题?

标头: x- AUTH - TOKEN : AUTH令牌内容类型:应用程序/x-www-form-urlencoded

更新:我得到的错误是:

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-06-04 05:32:21

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

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56434529

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档