2
fetchData () {
  axios.get('https://some-address.com/orders', {
    params: {
      limit: this.limit,
      offset: this.offset
    },
    headers: auth.getAuthHeader()
  })
    .then((resp) => {
      this.req = resp.data
      console.log(resp)
    })
    .catch((err) => {
      console.log(err)
    })
}

Storing token in "auth/index.js"

  // The object to be passed as a header for authenticated requests
  getAuthHeader () {
    return {
      'Authorization': localStorage.getItem('access_token')
    }
  }

When i send GET request to receive a list of items, i'm getting the following headers in response:

enter image description here

in request:

enter image description here

Status code 403, obviously API is not getting my access_token. But if i make the same request in Postman (manually pasting access token value) - it works.

Error from console:

enter image description here

Alexander Kim
  • 17,304
  • 23
  • 100
  • 157

2 Answers2

1

Try defining headers before:

axios.defaults.headers.common = {
    'Authorization' : localStorage.getItem('access_token')
};
Erubiel
  • 2,934
  • 14
  • 32
Max Maximilian
  • 591
  • 2
  • 20
  • 1
    I've already tried `axios.defaults.headers.common['Authorization'] = localStorage.getItem('access_token')` above `fetchData ()` in my case. No effect :( – Alexander Kim Aug 22 '18 at 10:53
  • Use axios interceptor for request and check if the Authorization is present in the headers before the request is made https://github.com/axios/axios#interceptors – Allkin Aug 22 '18 at 11:02
0

It was indeed issue on the backend side, thanks to @KirkLarkin, he pointed me in the right direction. I had to coordinate with backend developer for making correct headers for CORS requests.

Alexander Kim
  • 17,304
  • 23
  • 100
  • 157