Home > OS >  Getting no authorization header value error,
Getting no authorization header value error,

Time:11-30

Authorization Headers (bearer token) not getting adding into the api calls
Status is 401 Unautorized and headers are not getting added into the api call.

  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))
            }
        }  
    }

Getting Error

"status":401,"data":{"message":"Unauthorized, **no authorization header value**"}}

data: error.response?.data

not sure what i am missing here in the code

CodePudding user response:

You need to put the options as second argument of the get method, not after closing it.

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

CodePudding user response:

Updated the @reyno answer with bold

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