简体   繁体   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 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 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 returns 200 Curl 返回 200

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

On axios, header key must be inside the object.在 axios 上,header 密钥必须在 object 内。 Your header variable not included that 'header' as key.您的 header 变量不包括该“标题”作为键。 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)
  })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Axios 错误 401(未经授权)尝试获取 api 时出错 - Axios Error 401 (Unauthorized) Error when trying to fetch api AJAX Jquery 调用返回 401(未经授权)- API 调用 - AJAX Jquery Call returning 401 (unauthorized) - API Call 带有 OMDB API 的 Axios,得到 401 Unauthorized - Axios with OMDB API, getting 401 Unauthorized cURL命令有效,但是Fetch API调用在响应有效载荷中返回200并显示400验证错误 - cURL command works, but Fetch API call returns 200 with 400 validation error in Response Payload 在启用了窗口身份验证的情况下对asp.net Web API端点进行Ajax调用时出现401未经授权的错误 - 401 Unauthorized error when making ajax call to asp.net web api endpoint with window authentication enabled 使用 Axios 和 Firebase 时出现 401 未经授权的错误 - 401 Unauthorized Error Using Axios and Firebase 未经授权的AJAX请求返回状态码200,而不是401 - Unauthorized AJAX requests returning statuscode 200 instead of 401 反应 axios 401 未授权 - React axios 401 unauthorized 使用Google Calendar API返回401(未经授权) - Using Google Calendar API returns 401 (unauthorized) 使用 axios 执行经过身份验证的请求时,Jest 返回“网络错误” - Jest returns "Network Error" when doing an authenticated request with axios
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM