2

I am using json-server at localhost:3000. it is working fine and I am able to get data in Angular http.get. I want to read response header X-Total-Count, inside angular .subscribe. But I can't see that property in the response header. But Chrome console shows that X-Total-Count: 16.

this.catgoryItems.getAllServices(
    {
        _page: pageIndex || this.pageIndex,
        _limit: limit || this.limit
    })
    .subscribe((response: any) => {
        console.log(response);
        this.dataSource = response.body;
    }, err => {
        console.log(err);
        this.alertService.showError('Error, plz try again later');
    });
Vy Do
  • 46,709
  • 59
  • 215
  • 313
raju
  • 6,448
  • 24
  • 80
  • 163
  • Possible duplicate of [Read response headers from API response - Angular 5 + TypeScript](https://stackoverflow.com/questions/48184107/read-response-headers-from-api-response-angular-5-typescript) – SiddAjmera Sep 19 '18 at 04:53
  • https://stackoverflow.com/questions/44292270/angular-4-get-headers-from-api-response https://stackoverflow.com/questions/48184107/read-response-headers-from-api-response-angular-5-typescript?rq=1 – SiddAjmera Sep 19 '18 at 04:53

2 Answers2

7

Observe response and access headers

http
  .get<any>('url', {observe: 'response'})
  .subscribe(resp => {
    console.log(resp.headers.get('X-Total-Count'));
  });

https://stackoverflow.com/a/48184742/2742156

Vy Do
  • 46,709
  • 59
  • 215
  • 313
Pratap A.K
  • 4,337
  • 11
  • 42
  • 79
0

you can get header using this

request.get(url).subscribe(data => {
      let count = data.headers.getAll('x-total-count');
}