Samuel Martineau Samuel Martineau - 5 months ago 42
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.

$http.post(url, 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

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 error.data is instanceof Blob and convert it to JSON

Comments