Armin
Armin

Reputation: 471

Angular 4 HttpClient Headers missing

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),
  });
}

Upvotes: 2

Views: 1570

Answers (1)

Armin
Armin

Reputation: 471

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).

Upvotes: 2

Related Questions