出现无授权标头值错误,

问题描述 投票:0回答:3

授权标头(不记名令牌)未添加到 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**"}}

数据:错误.响应?.数据

not sure what i am missing here in the code
javascript node.js api axios jestjs
3个回答
2
投票

您需要将选项作为

get
方法的第二个参数,而不是在关闭它之后。

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

1
投票

用粗体更新了@reyno 答案

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

0
投票

当提示标头授权错误时我该怎么办

© www.soinside.com 2019 - 2024. All rights reserved.