Samuel Martineau
Samuel Martineau

Reputation: 83

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?

Upvotes: 1

Views: 1204

Answers (1)

Aleksey L.
Aleksey L.

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

Related Questions