Angular 4.3.3 HttpClient JSON Parsing Error, GET just returns null

Viewed 1584

If I get invalid data from an HTTP request using the new HttpClient in Angular 4.3.3, such as this (extraneous commas):

{
  "a": "it is a",
  "b": "it is b",,
}

I get no errors, and the result is null

this.httpClientNew.get<any>('assets/mockjson.json').subscribe(
  (response) => {console.log("NEW RESPONSE:[" + response + "]")},
  (error) => {console.error(error)}
)

Using the old client I can get the JSON parsing error including the exact character where the problem is:

this.httpClientOld.get('assets/mockjson.json').map(
  (response) => {console.log("OLD RESPONSE:[" + response + "]");
    return response.json();
  }
).subscribe(
  (res) => {console.log(res)},
  (err) => {console.error(err)}
)

Which gives the nice error:

SyntaxError: Unexpected token , in JSON at position 39

Is there a way to get this detailed error message with the new Angular 4.3.3 HttpClient? Thanks.

2 Answers

You can use observe if you're interested in the whole Response:

 this.http.get('…', { observe: 'response' });

Hope it helps.

Related