1

使用 Angular2 和 dotcms,我试图在调用 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()));

好的,响应正文为空,但在 Chrome 上看到我可以看到所有响应标头

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,执行很好,我无法获得响应标头

ERROR TypeError: Cannot read property 'headers' of null
4

1 回答 1

3

如果您想要完整的响应,您可以observe在选项对象中设置属性以及标题。

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

在这种情况下,您将获得整个响应以及标题。

于 2018-02-15T12:52:35.027 回答