Vue-resource: How i can get response headers in interceptor?

Created on 17 Mar 2016  路  16Comments  路  Source: pagekit/vue-resource

subj.

sorry for spam...

Most helpful comment

Figured out. I'm using cors requests with Access-Control-Allow-Headers header, but completely forgot about Access-Control-Expose-Headers.

@aios I think you have the same issue. Just add this header on our backend side and add Authorization header in it's list.

All 16 comments

Would it be possible if you could close this if it's no longer an issue?

That no solved.

Ahh my apologies, well you could try this...

Vue.http.interceptors.push({
    response: function(response) {
        console.log(response.headers());
    }
});

On a side note, try and keep questions in the forums as this is the wrong place to be asking them.

Yep - but not get it.

@aios You don't understand or you don't get any headers?

I make a request to backend with JWT Auth. So i want refresh token on backend side and set Authorization header to Bearer newtoken
When i return response, in devtools - i have check newtoken - with vue-resource - no Authorization header in interceptor and no in code

I solve another way - with set new token in response.data - but i want set in header and get it with vue-resource - to update JWT in interceptor.

I thought that might be where you were going wrong, you need to be getting tokens from response.data rather than in headers.

Vue.http.interceptors.push({

    request: function(request) {

        var headers = request.headers;

        if (!headers.hasOwnProperty('Authorization')) {
            headers['Authorization'] = 'TOKEN';
        }

        return request;
    }
});

Then you can use a request interceptor to inject the token into the request (_i am storing it in the applications state via vuex_).

You do not understand. My Vue app doesnt first step of update jwt. It doesnt make request for that.
I have backend on laravel. And have middleware jwt.refresh. That is automatic refresh token when it invalid - and set header Authorization to response. With response i want get it Auth Token with vue-resource and set to headers on vue app.

Which you can do by combining my previous comments.

  • Get the response header using the response interceptor.
  • Store the token.
  • Inject the token in future requests with the request interceptor OR with Vue.http.headers.common['Authorization'] = 'TOKEN'.

So i make proof. Hold on 15 minutes.

I have exactly the same problem. In response my api sends me these headers:

Access-Control-Allow-Origin:http://localhost:8080
Authorization:Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjEsImlzcyI6Imh0dHA6XC9cL2FuZ2VsaWEuZGV2XC9wcm9qZWN0cyIsImlhdCI6MTQ1ODY1MzA4NiwiZXhwIjoxNDU4NjUzMTQ2LCJuYmYiOjE0NTg2NTMwODYsImp0aSI6ImE5NTAxNGRlYmM4N2YyZGM1YzJkNmI0YzgxNDEyYTRlIn0.rmy4i0ze5xTLRv4AjCGhAUbDiQfDlMZj7vaNAjFsees
Cache-Control:no-cache
Connection:keep-alive
Content-Type:application/json
Date:Tue, 22 Mar 2016 13:24:46 GMT
Server:nginx/1.9.11
Transfer-Encoding:chunked
Vary:Origin

But my response interceptor shows me this:

response (response) {
    console.log(response.headers(), response.headers('authorization'))
}
Object {content-type: "application/json", cache-control: "no-cache", "": ""}
undefined

As far as I can see, problem is in xhr.getAllResponseHeaders() function. It returns only these two headers. Strange...

Figured out. I'm using cors requests with Access-Control-Allow-Headers header, but completely forgot about Access-Control-Expose-Headers.

@aios I think you have the same issue. Just add this header on our backend side and add Authorization header in it's list.

@mlanin Thanks!
!Solved!

@aios @mlanin thanks 锛宨t solved my problem

Was this page helpful?
0 / 5 - 0 ratings

Related issues

EmilMoe picture EmilMoe  路  5Comments

gbhlwm picture gbhlwm  路  5Comments

V-Tom picture V-Tom  路  3Comments

nivv picture nivv  路  4Comments

mikeyao picture mikeyao  路  6Comments