axios call to git api returns 401, but curling the same url with the same headers returns 200 with the correct data, anybody know why?
let headers = {
"Authorization": "token abc",
"Accept" : "application/json",
"Content-Type": "application/json"
}
await axios.get("SOME_GIT_API_URL", headers)
.then((data) => {
console.log(data.data)
})
.catch((err) => {
console.log(err)
})
curl --location --request GET "SOME_GIT_API_URL" --header 'Authorization: token abc'
On axios, header key must be inside the object. Your header variable not included that 'header' as key. Example:
// header inside an object
const headerObject = {
header: {
"Authorization": "token abc",
"Accept" : "application/json",
"Content-Type": "application/json"
}
}
await axios.get("SOME_GIT_API_URL", headerObject)
.then((data) => {
console.log(data.data)
})
.catch((err) => {
console.log(err)
})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.