响应为空时如何获得响应Headers?

How to get Response Headers when response is empty?

使用 Angular2 和 dotcms,我试图在调用订阅后获得响应 headers。

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()));

好的,回复 body 是空的,但在 Chrome 上看到我可以看到所有回复 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

我得到了200,执行没问题,我无法得到响应headers

ERROR TypeError: Cannot read property 'headers' of null

如果您想要完整的回复,您可以在选项 object 中设置 observe 属性 以及 headers.

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

在这种情况下,您将获得完整的响应以及 headers。