Faizan Saiyed
Faizan Saiyed

Reputation: 852

AXIOS : Send Authorization header returns Error 401, Authorization Header missing

I came around this solution but this is not working for me.

Following is my code:

axios.post('http://myurl/api/login', {
  email: '[email protected]',
  password: '123456'
}, {
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
  }
}).then(response => {
  if (response.data) {
    this.AuthToken = response.data.token
    console.log(this.AuthToken)
    axios.get('http://myurl/userdetails/:uid', {
      uid: 'D123'
    }, {
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Authorization': this.AuthToken
      }
    }).then(response => {
      if (response.data) {
        // this.AuthToken = response.data
        console.log(response.data)
      }
    }).catch(error => {
      console.log('User Data response Error: ' + error)
    })
  }
}).catch(error => {
  console.log('Login Error: ' + error)
})

I'm getting token from the first POST Login API call. I used that toke to pass into another API call as Authentication token. But I get error: Missing Authorization Headers

Upvotes: 0

Views: 1397

Answers (2)

Faizan Saiyed
Faizan Saiyed

Reputation: 852

Found the solution:

axios.defaults.headers.common['Authorization'] = this.AuthToken;

Upvotes: 3

Matija Župančić
Matija Župančić

Reputation: 1140

Try to add another header. "Access-Control-Allow-Headers" : "*".

Upvotes: 0

Related Questions