Home > other >  React-Axios Getting error 401 (Unauthorized) for PUT Method
React-Axios Getting error 401 (Unauthorized) for PUT Method

Time:12-12

I have gone through many posts and tested most of them but I still get this error. It's interesting that I don't get this error for the get method. I get this error only for put method. thanks to everyone

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);
      });

There is no error for the get method, but it gives an error for the put method I tested it with Postman and it works without errors

CodePudding user response:

There is something wrong with your Authorization header. You're using the right syntax (see MDN):

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

Given userToken is your <authorization-parameters>. The problem comes from the <auth-scheme> for which you used Token: this is not a valid scheme according to the list maintained by IANA.

Hard to be sure without much information, but I guess what you want is:

Authorization: `Bearer ${userToken}`,

CodePudding user response:

My problem was solved with this format:

axios({ method: "put", url: "api/v1/Profile/", data: { fullName: userName, gender: Number(userGender), phoneNumber: "" }, headers: { "Content-Type": "application/json; charset=UTF-8", Authorization: Bearer ${userToken}, }, }).then((response) => { console.log(response);

    });  
  • Related