Angular 4 get headers from API response

后端 未结 5 1286
我在风中等你
我在风中等你 2020-12-06 05:50

I\'m sending a request to an API, it returns an array of data, but I don\'t know how to extract the headers from that url, this is what i\'ve tried in my service

<         


        
5条回答
  •  感情败类
    2020-12-06 06:02

    Clear angular 5 answer

    By default, this.http.whatever's returned observable will be on the data returned, not the HttpResponse.

    If you have a peak at: https://angular.io/api/common/http/HttpClient You'll notice the options take an "observe" parameter of a HttpObserve type. While it's not documented what the HttpObserve is, if you put it as "response" then you will instead receive an instance of HttpResponse(https://angular.io/api/common/http/HttpResponse)

    So, here's an example request:

    this.http.get(url, {observe: 'response'})
        .subscribe(resp => console.log(resp.headers))
    

    Note: Due to browser cors security, you will not be-able to see headers unless the API provides Access-Control-Expose-Headers: with your custom headers if your api and angular app do not have the same domain.

提交回复
热议问题