I've been fetching some data from a private API with axios
, but now I'm having a problem fetching data from one specific endpoint.
The interesting thing is that with the built in fetch
API, I'm receiving 200 response, but the identical request with axios
keeps retuning 401 error. Any idea what can be the problem?
This code works:
const upVoteCommentTwo = async () => {
console.log(localStorage.getItem("access_token"));
try {
const response = await fetch(
`/${mentId}/vote/up`,
{
method: "POST",
headers: {
"X-API-KEY": "XXX",
"Authorization": localStorage.getItem("access_token"),
},
}
);
console.log(await response.status);
} catch (err) {
console.log(err);
}
};
And this does not work:
const upVoteCommentOne = async () => {
console.log(localStorage.getItem("access_token"));
try {
const response = await axios.post(
`/${mentId}/vote/up/`,
{
headers: {
"Content-Type": "application/json",
"X-API-KEY": "XXX",
"Authorization": localStorage.getItem("access_token"),
},
}
);
console.log(response.status);
} catch (err) {
console.log(err);
}
};
I've been fetching some data from a private API with axios
, but now I'm having a problem fetching data from one specific endpoint.
The interesting thing is that with the built in fetch
API, I'm receiving 200 response, but the identical request with axios
keeps retuning 401 error. Any idea what can be the problem?
This code works:
const upVoteCommentTwo = async () => {
console.log(localStorage.getItem("access_token"));
try {
const response = await fetch(
`https://exammple./ments/${mentId}/vote/up`,
{
method: "POST",
headers: {
"X-API-KEY": "XXX",
"Authorization": localStorage.getItem("access_token"),
},
}
);
console.log(await response.status);
} catch (err) {
console.log(err);
}
};
And this does not work:
const upVoteCommentOne = async () => {
console.log(localStorage.getItem("access_token"));
try {
const response = await axios.post(
`https://example./ments/${mentId}/vote/up/`,
{
headers: {
"Content-Type": "application/json",
"X-API-KEY": "XXX",
"Authorization": localStorage.getItem("access_token"),
},
}
);
console.log(response.status);
} catch (err) {
console.log(err);
}
};
Share
Improve this question
edited Jun 28, 2022 at 8:25
Youssouf Oumar
46.6k16 gold badges103 silver badges105 bronze badges
asked May 23, 2022 at 4:51
MaikyMaiky
692 silver badges7 bronze badges
0
2 Answers
Reset to default 5The problem is ing from axios
's parameters. One valid way, according to the doc and that seems to be working for most people according to this issue on GitHub is to call it like below, with an object:
const response = await axios({
method: "post",
url: `https://example./ments/${mentId}/vote/up/`,
headers: {
"Content-Type": "application/json",
"X-API-KEY": "XXX",
"Authorization": localStorage.getItem("access_token"),
},
data: {/* Your Data Goes Here */},
});
const upVoteCommentOne = async () => {
console.log(localStorage.getItem("access_token"));
try {
const response = await axios.post(
`https://example./ments/${mentId}/vote/up/`,
{
//body:...if any
}
headers: {
"X-API-KEY": "XXX",
"Authorization": localStorage.getItem("access_token"),
},
}
);
console.log(response.status);
} catch (err) {
console.log(err);
}
};
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745379028a4625136.html
评论列表(0条)