Vue-resource: Passing headers

Created on 14 Jun 2015  路  4Comments  路  Source: pagekit/vue-resource

Hi,

I've been trying to pass an authorization header to the http request and it doesn't seem to be working. I'm not entirely sure if this is the right way to do it, perhaps you know of a way to achieve this?

new Vue({

  el: '.container',

  ready: function() {

    this.fetchNode();

  },

  methods: {
    fetchNode: function() {

      this.$http.headers.common.Authorization = 'Basic YXBpOnBhc3N3b3Jk';

      this.$http.get('/node/1', function(node) {
        this.$set('node', node);
      });

    }
  },

});

Thanks!

Most helpful comment

You can set headers through the global config:

Vue.http.headers.common['Authorization'] = 'Basic YXBpOnBhc3N3b3Jk';

or when using the $http service through the options:

this.$http.get('/node/1', function(node) {
   // code ...
}, {headers: {'Authorization': 'Basic YXBpOnBhc3N3b3Jk'}});

All 4 comments

You can set headers through the global config:

Vue.http.headers.common['Authorization'] = 'Basic YXBpOnBhc3N3b3Jk';

or when using the $http service through the options:

this.$http.get('/node/1', function(node) {
   // code ...
}, {headers: {'Authorization': 'Basic YXBpOnBhc3N3b3Jk'}});

Is there any way to remove the authorization field, or clear the common attribute?

How to use headers with then ?
I don't know, where need setting this argument ...

By example, in this code:

          submitForm: function() {
            this.$http.post(this.submitendpoint, this.checkedItems).then(
               function(response){
                alert(response.data)
            }, function(error){
                console.log(this.$http.headers.common['X-CSRF-TOKEN'])
                alert(error.data)
            })
          },

If I try set common['X-CSRF-TOKEN'] globaly, this works, but i want to repeat this in the function. ?
How can i do this ?

Thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ramstein74 picture ramstein74  路  3Comments

mikeyao picture mikeyao  路  6Comments

Rachelin picture Rachelin  路  6Comments

christophwolff picture christophwolff  路  6Comments

nivv picture nivv  路  4Comments