4

HttpResponse is a generic class in @angular/common/http, so I need to mention a type e.g. HttpResponse<T> (replace 'T' with data type). Now that I am replacing old angular code from this:

.map((res: Response) => res.json());

to this:

.map((res: HttpResponse<any>) => res.json());

as class Response is deprecated (whole package @angular/http is deprecated). But this is giving me an error as below:

Property 'json' does not exist on type 'HttpResponse<any>'.

So I believe I have to change the type, but to what? Moreover, writing it like that is correct or not?

Ankur Shah
  • 801
  • 1
  • 12
  • 26
  • 2
    The httpClient parses your JSON to an object so this line is not needed `.map((res: HttpResponse) => res.json());` – AT82 Nov 21 '17 at 05:18
  • @AJT_82 : then can you please tell me, where and how to use `HttpResponse` in angular, as I haven't got any info regarding its usage on internet, just looking at docs don't give much idea – Ankur Shah Nov 21 '17 at 05:24
  • 1
    What are you trying to achieve? If you are looking to type your data, that is done like `return this.httpClient.get(.....)` https://angular.io/guide/http#typechecking-the-response – AT82 Nov 21 '17 at 05:29
  • 1
    try res.body to get the data inside body of the response. refer this link for documentation https://angular.io/api/common/http/HttpResponse – Prithivi Raj Nov 21 '17 at 05:30

0 Answers0