angulartypescriptputdotcms

How to get Response Headers when response is empty?


Working with Angular2 and dotcms, I'm trying to get response headers after calling subscribe.

const httpOptions = {
  headers: new HttpHeaders({ 'Content-Type': 'application/json' })
  .append('Access-Control-Expose-Headers', 'X-Custom-header')
};
let json = {..};
let apiURL = 'api/content/publish.1'
this.http.put(apiURL, JSON.stringify(json), httpOptions).subscribe((res:Response) => console.log(res.headers.keys()));

ok, response body is empty but seeing on Chrome I can see all response headers

HTTP/1.1 200 OK
X-Powered-By: Express
access-control-allow-origin: *
server: Apache-Coyote/1.1
location: http://localhost:3000/api/content/inode/bb23a6ac-f6f0-4ed3-8971-095dbc38ef52
inode: bb23a6ac-f6f0-4ed3-8971-095dbc38ef52
identifier: c05e9b20-46a4-4efc-9ded-b5d1a2613cad
access-control-allow-methods: GET, HEAD, POST, PUT, DELETE, OPTIONS
access-control-allow-headers: Authorization, Accept, Content-Type, 

Cookies
    content-length: 0
    date: Thu, 15 Feb 2018 12:34:27 GMT 
connection: close

I got 200, execution is fine, I can't get response headers

ERROR TypeError: Cannot read property 'headers' of null

Solution

  • If you want the full response you can set observe property inside options object along with headers.

    this.http.put(apiURL, JSON.stringify(json), 
     {observe:'response'}).subscribe((res:Response) => 
      console.log(res));
    

    In this case you will get whole response along with headers.