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),
  });
}
Armin
  • 441
  • 4
  • 20
  • 1
    in order to get the full response in a get you must include {observe: 'response'} (see https://angular.io/guide/http) I don't know if when we use a post you can acheive it if we use the same tecnic – Eliseo Nov 26 '17 at 14:38
  • @Eliseo we use `observe: 'response'`... – Armin Nov 26 '17 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. – JB Nizet Nov 26 '17 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. – Armin Nov 26 '17 at 14:56
  • 1
    This is *probably* your issue: https://stackoverflow.com/questions/14686769/xmlhttp-getresponseheader-not-working-for-cors. But of course, with so few information about your code, it's only a guess. https://stackoverflow.com/questions/14686769/xmlhttp-getresponseheader-not-working-for-cors. – JB Nizet Nov 26 '17 at 14:57
  • I am actually facing the same issue. Tried everything but no solution : https://stackoverflow.com/questions/47497390/angular2-service-request-header-response-for-x-pagination-page-count/47497617#47497617 please let me know if you found solution for this. – Anil Kumar Nov 26 '17 at 15:48

1 Answers1

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

Armin
  • 441
  • 4
  • 20