Search code examples
angularjson-server

How to read X-Total-Count from json-server in Angular http.get?


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');
    });

Solution

  • 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