【问题标题】:Getting issue on API call where I am returning response like Unauthorized在我返回未授权之类的响应时遇到 API 调用问题
【发布时间】:2020-01-20 02:09:24
【问题描述】:

我正在尝试使用 axios 调用 api。我正确传递了所需的参数和标题,但在 api 命中时,我将响应作为 api 错误返回:{“message”:“Unauthorized”}。我尝试了太多解决方案,例如使用承载、jwt 令牌,我还将 API 调用库更改为 fetch 但仍然没有成功。

axiosGetWithHeaders('url',
      {
        param1: 'emailid'
      },{
        Authorization : 'token',
        ContentType: 'application/json',
      }

    ).then((res) => {
        console.log("RESPONSE", JSON.stringify(res))
      })
      .catch((error) => {
        console.error('api error: ' + JSON.stringify(error));
        console.error('api error: ' + JSON.stringify(error.response.data));


      });

【问题讨论】:

  • 什么样的api?你能发布带有地址的真实样本吗?
  • 这是 aws cognito api
  • 我没有从这个链接@Oleg 中得到任何东西。我在邮递员中使用了相同的参数,标题它工作正常

标签: react-native axios amazon-cognito


【解决方案1】:

用 axios 试试:

axios.defaults.headers.common["Authorization"] = accessToken;`enter code here`

【讨论】:

  • 使用 axios.defaults.headers.common["Authorization"] 仍然没有成功。我昨天试过了。我现在仍然检查它但没有成功
  • 验证你是否有有效的令牌,你能提供带有令牌的真实样本在codesandbox中会更好
  • 我如何验证这一点。因为无论我在控制台中打印什么,我在 Postman 中使用的东西都可以正常工作
  • 您可以在邮递员中生成 Generate code sn-ps for javascript 如果它工作:learning.getpostman.com/docs/postman/sending_api_requests/… 并使用它验证您的代码。
  • 获取 url 的 params 对象有问题
猜你喜欢
  • 2016-03-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-01
  • 2021-04-16
  • 2017-03-20
  • 2015-10-04
相关资源
最近更新 更多