2

My problem is that I cannot get custom HTTP headers from my server into angular 4. When I query the endpoint from another machine with CURL (screenshot 1), I get the header (authorization) but when I try accessing it via res.headers.get in Angular, I get null (screenshots 2 and 3).

The backend runs at localhost:1337 and the frontend runs at localhost:4200

Screenshot 1: screenshot 1

Screenshots 2 & 3: Screenshot 2 screenshot 3

The used postData method:

postData<T>(url, body) {
  return this.http.post<T>(this.hostURL + url, body, {
    observe: 'response',
    responseType: 'json',
    headers: new HttpHeaders().set('Authorization', this.session),
  });
}
6
  • 1
    in order to get the full response in a get you must include {observe: 'response'} (see angular.io/guide/http) I don't know if when we use a post you can acheive it if we use the same tecnic Commented Nov 26, 2017 at 14:38
  • @Eliseo we use observe: 'response'... Commented Nov 26, 2017 at 14:47
  • @Armin post all the rlevant code, as text. And post the output you get, as text. Not sure why you expect the server to send an Authorization heaer anyway. That's a header that the client should send, containing the authorization credentials to access the protected resources. If the server needs to send an authorization token, it should send it back in the body. Commented Nov 26, 2017 at 14:49
  • @JBNizet added the postData method. We send the authorization header back and forth. They run out after some time and to prevent this when you are still working we refresh the token after every request. Commented Nov 26, 2017 at 14:56
  • 1
    This is probably your issue: stackoverflow.com/questions/14686769/…. But of course, with so few information about your code, it's only a guess. stackoverflow.com/questions/14686769/…. Commented Nov 26, 2017 at 14:57

1 Answer 1

2

I found the problem:

I was missing the Access-Control-Expose-Headers attribute in my server. If you are using Actionhero, you have to add your custom headers to this attribute which has to be added at httpHeaders in config/servers/web.js (at least if you are using this config).

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

Comments

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.