1

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
MJ-79
  • 133
  • 3
  • 10

1 Answers1

3

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.

Ankita Gupta
  • 573
  • 3
  • 13
  • I'm still getting null const httpOptions = { headers: new HttpHeaders({ 'Content-Type': 'application/json' } .append('Access-Control-Expose-Headers', 'X-Custom-header'), observer: 'response' }; – MJ-79 Feb 15 '18 at 13:30
  • 1
    Key is observe not observer and you forgot to end parenthesis before append().`const httpOptions = { headers: new HttpHeaders({ 'Content-Type': 'application/json' }) .append('Access-Control-Expose-Headers', 'X-Custom-header'), observe: 'response' }; ` – Ankita Gupta Feb 15 '18 at 13:51
  • Thank you very much for your help! **Now it works** this.http.put(apiURL, JSON.stringify(json), { headers: new HttpHeaders({ 'Content-Type': 'application/json' }).append('Access-Control-Expose-Headers', 'X-Custom-header'), observe: 'response' }) but if I move all options to a const I got "Type 'string' is not assignable to type '"body"'. – MJ-79 Feb 15 '18 at 14:56