Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add extra header to post request with vue-resource?

I have a number of post requests in my app. Some of them have to have and extrace header with token I'm not sure how to append it

So far my code is like this. I'm checking if there is a token and when append it to headers and after that I make post request with vue-resource post method.

let headers = new Headers({'Content-Type': 'application/json;charset=utf-8'});

 if(token !== '') {
    headers.append('TOKEN', token);
  }

  return this.http.post(uri, data, headers)
         .then(this.extractData)
         .catch(this.handleError);

But this doesn't append TOKEN

What does work is this

this.http.interceptors.push(function(request) {
                request.headers.set('TOKEN', token);
            });

at the place of headers.append('TOKEN', token);

But for some reason it pushes TOKEN header not for certain requests but for all of them

So when I'm make request with token - it works fine, after that I make request witthout token but it is still adds it.

Anyone knows what is the best way to fix this?

UPD If I console.log(headers.get('TOKEN')) while doing headers.append('TOKEN', token); it gives me the right value. So my guess is that post request itself is called with wrong headers.

like image 699
Person Avatar asked Aug 03 '18 06:08

Person


People also ask

How do I add a header to a request?

To add custom headers to an HTTP request object, use the AddHeader() method. You can use this method multiple times to add multiple headers. For example: oRequest = RequestBuilder:Build('GET', oURI) :AddHeader('MyCustomHeaderName','MyCustomHeaderValue') :AddHeader('MySecondHeader','MySecondHeaderValue') :Request.

How do you make a header on Vue?

For adding header to the Card , you need to create wrapper div element with e-card-header-caption class. Place the div element with e-card-header-title class inside the header caption for adding main title. Place the div element with e-card-sub-title class inside the header caption element for adding sub-title.

How do you use the Vue interceptor?

Intercepting a request We are using vue-resource http client package for intercepting requests in vuejs. import Vue from "vue"; import App from "./App. vue"; import VueResource from 'vue-resource'; // telling vue. js to use this package Vue.


1 Answers

Vue.http.headers.common['Authorization'] = "Basic Token";
like image 149
Alok Chaudhary Avatar answered Oct 20 '22 15:10

Alok Chaudhary