【发布时间】:2022-01-06 10:42:05
【问题描述】:
授权标头(不记名令牌)未添加到 api 调用中
状态为 401 Unautorized,并且标头未添加到 api 调用中。
const axios = require('axios')
let token = 'eyJ0eXAiOiJOiJIUzI1NiJ9.eyJpc3MiOiJJQ00iLCJhdWQiOiJzZXNzaW9uLW1hbm'
export class classname )
async getReports ()
{
let response
try {
response = await axios.get(`https://urltogo/path`), {
headers: {
'Content-Type' : 'application/json',
Authorization : `Bearer ${token}`
}
}
const responseObj = {
url: `GET ${`https://urltogo/path`}`,
status: response.status,
data: response.data
}
if (responseObj.data.meta.count == 1) {
return responseObj.data.items[0].id
}
} catch (error) {
const errorObj = {
status: error.response?.status,
data: error.response?.data
}
throw new Error(JSON.stringify(errorObj))
}
}
}
遇到错误
"status":401,"data":{"message":"Unauthorized, **no authorization header value**"}}
数据:error.response?.data
not sure what i am missing here in the code
【问题讨论】:
-
我认为您需要在“授权”周围加上引号
标签: javascript node.js api axios jestjs