HttpClient: Can´t access to response headers

不问归期 提交于 2019-12-19 04:22:40

问题


In a project, we used both Http & HttpClient to fetch the header params. Http returns the header params, but HttpClient doesn't.

constructor(private http: Http, private httpClient: HttpClient) {}
getContent() {
        const url = '';
        return this.http.post(url, data)
            .map((res: HttpResponse<any>) => {
                console.log('http content', res);
            });


        return this.httpClient.post(url, data, { observe: 'response' })
            .map((res: HttpResponse<any>) => {
                console.log('httpClient content',res);
            });
}

When checked in console, http returns the response with headers but httpClient returns an empty array in headers.

When checked in networks tab of browser inspector, it displays all the header parameters.

The server accepts the following CORS options:

  origin: '*',
  methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
  allowedHeaders: 'Origin,X-Requested-With,x-access-token,Content-Type,Authorization,Accept,jwt',
  exposedHeaders: 'Content-Type,Authorization,jwt'

Please help me figure out how to fetch the headers using httpClient.

Thanks in advance.


回答1:


You are on the right track with the HttpClient. You should use the get() method on your headers.

this.httpClient.post(url, data, {observe: 'response'})
    .map((res: HttpResponse<any>) => {
        let myHeader = res.headers.get('my-header');
        return res;
    });

Where my-header is the header response you are trying to fetch.



来源:https://stackoverflow.com/questions/47389365/httpclient-can%c2%b4t-access-to-response-headers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!