Angular2 HTTP response body in error

this.http.put(url, data) .map(response => response.json()) .subscribe( response => console.log(response), error => console.log(error), ); 

If successful, it displays the data returned from the API. By mistake, the output is a ProgressEvent with a status of 0.

How to get response data from the API when an error occurs?

+7
source share
2 answers

You can probably try

 this.yourHttpCall().subscribe( val => { //do something }, err => { let error = (() => { try { return JSON.parse(err._body) } catch (something) { return err })() console.log(error); } ); 

Such a hack around. Not sure if it works for your endpoint.

0
source

You can check the _body property in the response, for example:

 this.http.put(url, data) .map(response => { if (response['_body']) { // check response here. return response.json() } else { return {} // or return null. } }) .subscribe( response => console.log(response), error => console.log(error), ); 
0
source

Source: https://habr.com/ru/post/1013724/


All Articles