Tuesday, March 27, 2018

Angular 5 HttpClient Assumes JSON

In Angular 4 (I think) HttpClient was introduced to make remote calls easier. I didn't have a problem using it until I needed to return raw HTML from a remote API (which I'll write about in another post because it was very interesting) and found out that HttpClient assumes the result of the remote call is in JSON format. I totally understand why they went that route, because JSON is the most common (and IMO the best) format for data returned from a remote API. I really needed raw HTML (content type text/html) so it was a problem for me.

Luckily, right there in the docs is the way you're supposed to handle this scenario. All we have to do is specify a second parameter that declares the expected response type. It's not ideal, but given that (at least in my experience) somewhere around 99% or remote calls return JSON I'm OK with this approach... now that I know the solution for the 1%.

No comments:

Post a Comment