res.json() is a not a function in HttpClient Angular 2
12,107
Right, that's because new http client by default calls res.json()
implicitly and you don't need to that manually yourself. Here is the quote from commit:
JSON is an assumed default and no longer needs to be explicitly parsed
See Difference between HTTP and HTTPClient in angular 4? for more details.
Author by
INFOSYS
Updated on June 05, 2022Comments
-
INFOSYS about 2 years
I was using Angular
Http
module before and the methodres.json()
used to work fine. I have recently triedHttpClient
but then theres.json()
dosen't seem to work . only usingres
works can some one tell me what changed has happened in the http client.return this.client.get('https://swapi.co/api/people/1/') .map((res:Response) => { return res.json(); // using maps to filter data returned form the http call this json dosn't work with http client }).map(data => { return data; // using maps of maps to filter data returned form the map }).flatMap((jedi) => this.http.get(jedi['homeworld']) .map(res => { return res.json().name; // using flat maps to combine data returned from two observables into one }).catch((error:any) => Observable.throw(error.json().error || 'Server error')));
I switched to http client because of the new interceptor can pointers are welcomed thanks
-
INFOSYS almost 7 yearsohh thats whats chabged thanks. So it always better now to use httpclien instead of http right?
-
Max Koretskyi almost 7 years@INFOSYS, yes, I believe so. It has many advantages. Check the answer I linked.
-
Yunnosch about 6 yearsPlease highlight the additional insight of your answer as compared to the existing, accepted and upvoted one.
-
Jitendra Ahuja about 6 yearsDear Yunnosch, there is nothing much to add into it, its that simple. I think the only pertinent factor in this answer is not to use .json() as it works automatically with HttpClient over simple http request.