简体   繁体   中英

git api returning 401 unauthorized error when doing axios call but curl returns 200

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?

Axios call returns 401

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 returns 200

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM