1

So I have a post like below, I send a POST request to login and the response is empty - which is fine, as expected - but I need to access the 'Authorization' header.

I didn't find any way to access.

  const data = {email: "email", password: "password"};

  this.http.post(`/login`, data).pipe(
    tap(resp => {
      console.log(resp); // it will be an empty string
      return resp;
    })).subscribe((resp: any) => {
    console.log(resp.headers.get('Authorization')); // error: Cannot read property 'headers' of null
  });

UPDATE Inspired by TheUnreal's answer I added options

this.http.post(`/login`, data, {observe: 'response'} )...

Now I can access some of the headers like 'Cache-Control'.

Probably I need to express the rest of the headers from serverside

Sam Walpole is right in the comment section, most part of the this question was answered on the link he provided - check out.

pszaba
  • 1,034
  • 9
  • 16

2 Answers2

0

You can get the headers and other information of the request by passing the following options to your http method:

return this.http.post('YOUR URL', PAYLOAD, {
      observe: 'events'
    })
TheUnreal
  • 23,434
  • 46
  • 157
  • 277
0

Add options to the post as TheUnreal suggested, but for me 'response' was needed

this.http.post(`/login`, data, {observe: 'response'} )

On server side, headers Access-Control-Allow-Headers and Access-Control-Expose-Headers need to be set and exposed.

A bit off topic but - may help someone - this is how our config looks like in Java Spring

configuration.setAllowedHeaders(Arrays.asList("Authorization", "Cache-Control", "Content-Type", "Access-Control-Allow-Headers", "Origin,Accept", "X-Requested-With", "Content-Type", "Access-Control-Request-Method", "Access-Control-Request-Headers"));
configuration.addExposedHeader("Authorization");

Sam Walpole is right in the comment section of this question, most part of the this question was answered on the link he provided - check out.

pszaba
  • 1,034
  • 9
  • 16