2

I have made an http interceptor in which I need to evaluate responese headers and find particular one (authorization). Interceptor itself is working however it is unable to list headers which I am setting on server side and which I can clearly see set in firebug's network tab.

interceptor:

app.factory('HttpItc', function($q, $localStorage, $injector) {
return {
response: function (response) {
   //console.log(response); // Contains the data from the response.

  var freshJwt = response.headers['Authorization'];

  if (freshJwt) {
    $localStorage.jwt = freshJwt;
  }

  // Return the response or promise.
  return response;
}  };});

headers in firebug:

evalueated response:

This is how I set headers in Express on the server side:

return res
                    .header('Authorization', jwt)
                    .header('testHeader', 'testValue')
                    .json(user);

What is happening? How can I get access to response headers in http's interceptor?

4
  • Can you try this please and see if you get any results console.log(response.headers('Content-Length')); Commented Feb 1, 2016 at 14:06
  • @stackg91 it's 'null'. But the Data object in that response is properly set and populated... Commented Feb 1, 2016 at 14:11
  • 1
    I think you can get the authorization headers by calling var jwt = response.headers('Authorization') Commented Feb 1, 2016 at 15:07
  • @themyth92, you were right. it should be accessed as function... also you need to have 'Authorization' header set as exposed on the backend side. thx Commented Feb 2, 2016 at 15:23

1 Answer 1

10

The solution is to call response.headers('...') (as a function, not an array).

Also be sure to explicitly expose headers on the back-end side.

EDIT: To explicitly expose headers on the back-end you would need to add something like

response.addHeader("Access-Control-Expose-Headers","yourHeaderName");

This is similar to adding CORS headers, and currently does not work with '*'

Sign up to request clarification or add additional context in comments.

3 Comments

I think it should be plural response.headers('...')
Not working '*' with expose-headers header made my day.
add "Access-Control-Expose-Headers: yourheader" on server (php, java, nodejs) works smoothly

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.