获取无授权 header 值错误,

Getting no authorization header value error,

授权 Headers(不记名令牌)未添加到 api 调用中
状态为 401 未自动且 headers 未添加到 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

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

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

用粗体更新了@reyno 的回答

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