On the server there is a cors filter. In chrome in the developer’s panel comes a response from the server with all the headers. For some reason, my code can't get the header and prints null.

login(user: User) { return this.http.post(this.loginURL, { "type": "User", "id": null, "login": user.login, "password": user.password }, {observe: 'response'} ) .subscribe(resp => { console.log(resp.body['token']); console.log(resp.headers.get('Authorization')); } ); } 

body ['token'] is normally output to the console. A header can not be obtained.

  • Look at what you have in response (body) and get the value you need as follows: resp.body.token - GVArt
  • ?? Well I wrote that the body is already displayed. And headers can not get ( - andreyatake

2 answers 2

Still, the problem was in the cors filter on the server side. For some reason, in all examples of such filters on the Internet, it was not mentioned that it is necessary not only to resolve the necessary headers in the request, but also to indicate which headers we can process on the client.

It is necessary to add the header "Access-Control-Expose-Headers" in the response.

For example, this is how it looks in my filter response.setHeader (" Access-Control-Expose-Headers ", "x-requested-with, origin, date, content-Type, accept, authorization"); In it we list the titles we need.

    Not all headers are readable on the client side. Try typing all the headers on the console.

     console.log(response.headers); 

    If it comes, then you need to replace the header key on the server side.

    You can also try this (Set the type resp: Response):

     .subscribe((resp: Response) => { console.log(resp.headers.get('Authorization')); })