3

I'm trying to get the response headers from a post request, but the HttpResponse object doesn't contain the same headers that I can see in the network. What am I doing wrong? I need to access the value of the Apiproxy-Session-Id key and it isn't present in the HttpHeaders.

This is my code to execute the post request and log the full response, where http is an HttpClient object.

this.http.post('http://localhost:8081/user/login', JSON.stringify(requestBody), {observe: 'response'}).subscribe(resp => { console.log(resp); });

This is the response I'm logging.

These are the headers I'm seeing in the network.

I'm new to Angular and very stumped by this. Thanks for your help!

4

2 回答 2

4

嘿,伙计,我在这里遇到了同样的问题。由于您的后端和前端位于不同的域上,因此默认情况下不会公开响应的某些标头。

您可以尝试两种不同的方法:

1. 在 Angular 上代理您的请求

有了这个代理会让你的后端认为请求来自 sabe 域。请参阅此文档以了解如何操作。请记住,使用此选项将公开所有标题。

2. 在后端服务器上公开您想要的标头

由于我不知道您后端的语言,因此我无法逐步告诉您这样做,但是我必须在响应中执行以下操作:response.add("Access-Control-Expose-Headers", "Apiproxy-Session-Id").

于 2018-01-17T16:54:23.733 回答
0

尝试添加responseType: 'text'

this.http.post('http://localhost:8081/user/login', 
    JSON.stringify(requestBody), {observe: 'response', responseType: 'text'}).subscribe(resp => {
        console.log(resp);
    });
于 2018-01-08T08:56:30.843 回答