【问题标题】:React-Axios Getting error 401 (Unauthorized) for PUT MethodReact-Axios 获取 PUT 方法的错误 401(未经授权)
【发布时间】:2022-12-11 04:30:50
【问题描述】:

我浏览了很多帖子并测试了其中的大部分,但我仍然遇到此错误。 有趣的是,我没有为 get 方法收到此错误。我只为 put 方法收到此错误。 谢谢大家

axios error 401 (Unauthorized)

axios
      .put("/api/v1/Profile", {
        fullName: userName,
        gender: Number(userGender),
        phoneNumber: userNumber,
        headers: {
          Accept: 'application/json',
          "Content-Type": "application/json; charset=UTF-8",
          Authorization: `Token ${userToken}`,
        },
      })
      .then((response) => {
        console.log(response);
      })
      .catch((error) => {
        console.log(error);
      });

get方法没有报错,但是put方法报错 我用 Postman 测试了它,它没有错误

【问题讨论】:

    标签: reactjs api axios authorization put


    【解决方案1】:

    您的 Authorization 标头有问题。您使用的语法正确 (see MDN):

    Authorization: <auth-scheme> <authorization-parameters>
    

    鉴于userToken是你的&lt;authorization-parameters&gt;。问题来自您使用Token&lt;auth-scheme&gt;:根据list maintained by IANA,这不是有效方案。

    没有太多信息很难确定,但我想你想要的是:

    Authorization: `Bearer ${userToken}`,
    

    【讨论】:

      猜你喜欢
      • 2018-02-23
      • 1970-01-01
      • 2019-07-10
      • 1970-01-01
      • 2019-12-12
      • 1970-01-01
      • 2015-04-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多