70

I'm trying to use axios for a GET request with an API which requires an Authorization header.

My current code:

const AuthStr = 'Bearer ' + USER_TOKEN;

where USER_TOKEN is the access token needed. This string concatenation may be the issue as if I post this as AuthStr = 'Bearer 41839y750138-391', the following GET request works and returns the data I'm after.

axios.get(URL, { 'headers': { 'Authorization': AuthStr } })
  .then((response => {
    console.log(response.data);
  })
  .catch((error) => {
    console.log(error);
  });

I also tried setting this as a global header with no success.

7
  • what does console.log('Bearer ' + USER_TOKEN) give? Commented Jan 7, 2017 at 7:47
  • 1
    It gives Bearer 472397403110 (or whatever token number) Commented Jan 7, 2017 at 7:48
  • 1
    what does console.log(typeof(USER_TOKEN)) give? Commented Jan 7, 2017 at 7:52
  • Try using concat. It is the recommended javascript way of string concatination Commented Jan 7, 2017 at 7:54
  • No change using concat. And typeof gives string return Commented Jan 7, 2017 at 7:59

2 Answers 2

91

For anyone else that comes across this post and might find it useful... There is actually nothing wrong with my code. I made the mistake of requesting client_credentials type access code instead of password access code (#facepalms). FYI I am using urlencoded post hence the use of querystring.. So for those that may be looking for some example code.. here is my full request

Big thanks to @swapnil for trying to help me debug this.

   const data = {
      grant_type: USER_GRANT_TYPE,
      client_id: CLIENT_ID,
      client_secret: CLIENT_SECRET,
      scope: SCOPE_INT,
      username: DEMO_EMAIL,
      password: DEMO_PASSWORD
    };



  axios.post(TOKEN_URL, Querystring.stringify(data))   
   .then(response => {
      console.log(response.data);
      USER_TOKEN = response.data.access_token;
      console.log('userresponse ' + response.data.access_token); 
    })   
   .catch((error) => {
      console.log('error ' + error);   
   });



const AuthStr = 'Bearer '.concat(USER_TOKEN); 
axios.get(URL, { headers: { Authorization: AuthStr } })
 .then(response => {
     // If request is good...
     console.log(response.data);
  })
 .catch((error) => {
     console.log('error ' + error);
  });
Sign up to request clarification or add additional context in comments.

3 Comments

how to pass multiple custom header in axios
axios.get(uri, { headers: { "custom-header-1": "value", "custom-header-2": "value" } })
Do you have a header example to do it with Amplify and Apigateway? :) just asking
7

Could not get this to work until I put Authorization in single quotes:

axios.get(URL, { headers: { 'Authorization': AuthStr } })

1 Comment

This shouldn't matter

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.