微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

邮递员 POST 有效,但不适用于轴

如何解决邮递员 POST 有效,但不适用于轴

通过 insomnia/postman 作品发出帖子请求,给了我正确的回应。但是,在 axios 中,我似乎无法使其工作。

它没有给我一个错误,它的状态是 200,但实际上并没有像邮递员/失眠那样登录。我觉得我的 axios 设置有问题

enter image description here

enter image description here

  const { email,password } = req.body.params
  const endpoint = `https://xxxxxxxxx.com/account/login/`

  try {
    const response = await axios({
      method: 'post',url: endpoint,data: qs.stringify({
        'customer[email]': email,'customer[password]': password,'form_type': 'customer_login'
      }),headers: {
        'content-type': 'application/x-www-form-urlencoded'
      }
    })
    res.status(200).send(response.data)
  } catch (error) {
    res.status(500).send(error)
  }

解决方法

有两种解决方案(我希望):

1/ 在 this post 之后,您可能应该像这样使用 withCredentials

  const { email,password } = req.body.params
  const endpoint = `https://xxxxxxxxx.com/account/login/`

  axios.defaults.withCredentials = true;

  try {
      (...)
  }

2/ 在 this article 之后,您需要将 Content-Type 标头设置为 application/json 以便自动解析数据:

Axios 还将 Content-Type 标头设置为 application/json。这使 Web 框架能够自动解析数据。

,

Postman 允许您生成使用 axios 重新创建请求所需的代码。

此处的更多信息:https://learning.postman.com/docs/sending-requests/generate-code-snippets/

点击 Postman 右侧的代码图标

Postman code button

然后将代码片段语言设置为 NodeJS - Axios,您将拥有重新创建请求所需的确切代码。 Axios code snippet

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?