【问题标题】:Trying to use axios to query an API endpoint尝试使用 axios 查询 API 端点
【发布时间】:2021-07-21 13:16:22
【问题描述】:

嘿,我正在尝试让这个 mod.io 示例正常工作。这是他们给出的示例 curl

curl -X POST https://api.mod.io/v1/oauth/emailrequest \
  -H 'Content-Type: application/x-www-form-urlencoded' \
  -d 'api_key=0d0ba6756d032246f1299f8c01abc424' \
  -d 'email=john.snow@westeros.com'

我正在尝试将其添加到我的 Vue JS 应用程序中,但它返回 401 错误。有人能看出什么不对吗?

methods: {
loginUser() {
  const headers = {
    'Content-Type': 'application/x-www-form-urlencoded',
  }
  console.log(this.email) //Works
  const data = {
    api_key: "0d0ba6756d032246f1299f8c01abc424",
    email: this.email
  }
  axios
  .post('https://api.mod.io/v1/oauth/emailrequest', data, {
    headers: headers
  })
  .then(response => {
    console.log(response);
  })
  .catch((error) => {
    console.log(error);
  })
}

API 和电子邮件来自文档,因此请随意尝试。 This 是我感动的地方

我的本​​地主机可能不是 SSL 吗?

【问题讨论】:

    标签: javascript vue.js post http-post


    【解决方案1】:

    我知道我很接近......

      const headers = {
        'Content-Type': 'application/x-www-form-urlencoded'
      }
      const data = "api_key=0d0ba6756d032246f1299f8c01abc424&email="+this.email;
      
      axios
      .post('https://api.mod.io/v1/oauth/emailrequest', data, {
        headers: headers
      })
      .then(response => {
        console.log(response);
      })
      .catch((error) => {
        console.log(error);
      })
    

    【讨论】:

      猜你喜欢
      • 2013-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多