簡體   English   中英

git api 在執行 axios 調用時返回 401 未經授權的錯誤,但 ZF6E57C9DE7009E45FEB048D95353

[英]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 調用返回 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 返回 200

curl --location --request GET "SOME_GIT_API_URL" --header 'Authorization: token abc'

在 axios 上,header 密鑰必須在 object 內。 您的 header 變量不包括該“標題”作為鍵。 例子:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM