【问题标题】:Getting no authorization header value error,没有授权标头值错误,
【发布时间】: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


【解决方案1】:

您需要将选项作为get 方法的第二个参数,而不是在关闭它之后。

response = await axios.get(`https://urltogo/path`, {
  headers: {
    'Content-Type' : 'application/json',
    Authorization : `Bearer ${token}`                   
  }
});

【讨论】:

  • 感谢@Reyno 成功了
【解决方案2】:

用粗体更新了@reyno 的答案

 response = await axios.get**(**`https://urltogo/path`,{
                    headers: {
                        'Content-Type' : 'application/json',
                        'Authorization' : `Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ`}
                } **)**;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-30
    • 2014-12-25
    • 2017-11-23
    • 2021-12-15
    • 2019-03-20
    • 1970-01-01
    • 2011-06-23
    • 2020-01-05
    相关资源
    最近更新 更多