Reputation: 83
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?
Upvotes: 1
Views: 1204
Reputation: 37986
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
Upvotes: 1