How to pass headers on request with Vue.js 2

Asked

Viewed 1,168 times

3

I need to pass the header Authorization which is located in the localStorage with the key to iflix-user-token, here is my code:

getFilmes: function () {
    this.$http.get(Api.url + '/filme').then(
        response => {
            this.filmes = response.body
        }
    )
}

I’m using the vue-resource.

1 answer

1

Taking a look at Vue-Resource documentation, you can pass headers like this:

getFilmes: function () {
    const token = localStorage.getItem('iflix-user-token');
    this.$http.get(Api.url + '/filme', {headers: {'Authorization': token}})
      .then(response => this.filmes = response.body);
}
  • I searched there even before researching here, but I did not succeed. However, I put Authorization without quotation marks, I will try now and return. Anyway; Thank you very much.

  • @Márciolucas in this case the quotes do not make a difference. Are you sure that is the name of the header you have to send?

  • Yes, absolutely. Really, it didn’t work...

  • @Márciolucas where you read that you need to pass this header?

  • Brother, I managed to find the bug. I passed the header. Solved. Thank you!

  • @Márciolucas great! If the answer helped you can click to mark as accepted.

  • I really wanted to, but since I’m new here my acceptance is not counted.

  • @Márciolucas accept does not depend on the vote. You can take a look here: https://pt.meta.stackoverflow.com/q/1078/129

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.