kctang
kctang

Reputation: 11202

responseType for Angular HttpClient that can be either a blob or JSON

I have a REST API that returns either:

  1. binary data as a blob; or
  2. JSON data when there's an error.

The first scenario works with responseType: 'blob' and the second works with responseType: 'json'.

Can I somehow set the responseType after getting the response (i.e. depends on HTTP status code, etc.).

Upvotes: 3

Views: 1294

Answers (1)

Andrey
Andrey

Reputation: 2078

You can not.

P.S. Same REST API method returns blob either json? really? Probably you can distinguish at frontend somehow, by some argument value or so and set 'responseType' accordingly.

Or always expect blob then try to parse it with filereader and check if there is JSON inside.

Or implement interceptor. You can adjust response headers and content then.

Upvotes: 1

Related Questions