javascript - git api 在执行 axios 调用时返回 401 未经授权的错误,但 curl 返回 200
问题描述
axios 对 git api 的调用返回 401,但是使用相同的标头卷曲相同的 url 返回 200 和正确的数据,有人知道为什么吗?
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)
})
卷曲返回 200
curl --location --request GET "SOME_GIT_API_URL" --header 'Authorization: token abc'
解决方案
在 axios 上,标题键必须在对象内部。您的标头变量未包含该“标头”作为键。例子 :
// 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)
})
推荐阅读
- javascript - axios req failed 错误:在 XMLHttpRequest.handleLoad 的结算 (settle.js:17) 处的 createError (createError.js:16) 请求失败,状态码为 404
- python - 列表索引从字符串转换为索引python
- python-3.x - AttributeError: 'list' 对象没有属性 'size' Hugging-Face 转换器
- asp.net - 剃须刀页面模型中的 [Authorize(Roles = "xxx")] 身份验证
- amazon-web-services - EC2 中附加卷的设备名称始终为 xvdX?
- laravel - 在 Laravel 8 中提取 Vue 会破坏 Bootstrap 5.0.0-beta2 菜单和下拉菜单
- python - 如何在Python中获取每个有序行的值的前3个元素
- javascript - 如何从目录中动态导入所有导出的函数?(节点)
- python - 在生成文件中使用 exec
- android - 如何删除 RecycleViewAdapter 中的 ViewHolders?