Samuel Martineau Samuel Martineau -5 years ago 292
AngularJS Question

Angular (1.5) handle JSON errors from $http called with responseType blob

I ran into an issue with $http called with responseType: blob.
I would like to handle errors response in JSON but I get blob data instead even if I set the response's content-type as 'application/json' on the server.

$, data, {responseType: 'blob'})
.then(function(response) {
// download the file (works fine)
.catch(function(error) {
// handle JSON response error

Is there any way to do that without trying to convert error data to object?

Answer Source

You can't have different content types for success and error cases, but you can manage conversion in single place. Write responseError interceptor where you can check if is instanceof Blob and convert it to JSON

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download