响应为空时如何获取响应标头?

时间:2018-02-15 12:40:39

标签: angular typescript put dotcms

使用Angular2和dotcms,我在调用subscribe后尝试获取响应头。

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

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

ERROR TypeError: Cannot read property 'headers' of null

我有200,执行很好,我无法获得响应标题

const string serviceAccountEmail = "someemailaddresss";

var cert = new X509Certificate2(@"key.p12", "abigsecret", X509KeyStorageFlags.Exportable);

        var credential = new ServiceAccountCredential(
            new ServiceAccountCredential.Initializer(serviceAccountEmail) {
                Scopes = new[] { "https://spreadsheets.google.com/feeds" }
            }.FromCertificate(cert));

1 个答案:

答案 0 :(得分:3)

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

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

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