Like the title says, I need to use PUT method with axios in react, but also I need to have authentication. I am using this api: https://gorest.co.in/ , and on the site as you can see for PUT mehtod, you are required to use access token which can be provided to you when you login onto the site. Now, I have done that, and I got my token, but somehow I still get 401 status code a.k.a I am unauthorized for PUT method, here is what I have done:
const [data, setData] = useState(null);
useEffect(() => {
axios
.put(
"https://gorest.co.in/public/v1/users/6",
{
name: "Test",
body: "This is an updated user",
},
{
headers: {
Authorization:
"my token, for privacy reasons I am deleting it, but it goes here",
},
}
)
.then((response) => {
setData(response.data);
});
});
This is my first time doing PUT method so I am really confused on what I am doing wrong here. If someone could help me, I would appreciate that.
CodePudding user response:
Do you have the word Bearer before your token? Like:
Authorization: Bearer <auth-token>
CodePudding user response:
How about this?
{
headers: {
Authorization: `Bearer ${myToken}`,
},
}
The website defines the header like this
Authorization: Bearer ACCESS-TOKEN