angular: HttpClient - HttpErrorResponse not json but blob
I’m submitting a…
[ ] Regression (a behavior that used to work and stopped working in a new release)
[ ] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question
Not sure if its a bug or intended behavior so can’t check bug although it seems like one.
Current behavior
I am sending http request like this:
return this.http.get(url, {
observe:'response',
headers: headers,
responseType:'blob'
})
Server responds by 400 Bad Request, application/json content-type header and in body it returns json object:
{
message: "some error message"
}
error property on HttpErrorResponse is of type Blob.
Expected behavior
I would expect HttpErrorResponse.error property to be json according to content-type server returned since documentation says:
The error property will contain either a wrapped Error object or the error response returned from the server. ref: https://angular.io/api/common/http/HttpErrorResponse
So, if it puts server returned body into HttpErrorResponse.error then it feels like it should obey servers content-type header describing that same body.
Minimal reproduction of the problem with instructions
Use request above.
What is the motivation / use case for changing the behavior?
Environment
@angular/cli: 1.4.2
node: 6.9.3
os: win32 x64
@angular/animations: 4.4.6
@angular/common: 4.4.6
@angular/compiler: 4.4.6
@angular/core: 4.4.6
@angular/forms: 4.4.6
@angular/http: 4.4.6
@angular/platform-browser: 4.4.6
@angular/platform-browser-dynamic: 4.4.6
@angular/router: 4.4.6
@angular/cli: 1.4.2
@angular/compiler-cli: 4.4.6
@angular/language-service: 4.4.6
typescript: 2.3.4
Browser:
- [ ] Chrome (desktop) version XX
- [ ] Chrome (Android) version XX
- [ ] Chrome (iOS) version XX
- [ ] Firefox version XX
- [ ] Safari (desktop) version XX
- [ ] Safari (iOS) version XX
- [ ] IE version XX
- [ ] Edge version XX
For Tooling issues:
- Node version: XX
- Platform:
Others:
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 50
- Comments: 15 (5 by maintainers)
Links to this issue
Commits related to this issue
- fixing angular issues https://github.com/angular/angular/issues/19888 — committed to latusikl/PhotoPlus by syphzur 4 years ago
I created this interceptor as a temporary solution until this one is fixed:
Here’s what I did to handle json errors with a blob response type:
I really like this idea. Since
HttpErrorResponse.erroris typed asanyalready, it makes sense that it would depend on the server’sContent-Typefor the response.I’ll look into implementing this.
JaapMosselman wrote:
I’m posting an updated version using rxjs v6.3.3 (
_throwis nowthrowError), plus all the needed imports.Is there for now a simple workaround to get the json from the blob?
Is it correct that i’m still having difficulties receiving plain text using Angular 10
using (something like): const options = { responseType: ‘text’ as ‘json’, // !!! ??? headers };
I have tried several combinations of get<type> and responseType: ‘text’
(I have not tried any of the workarounds mentioned above …)
@marcosdimitrio do you have a synchronous version for this? I mean reading the Blob synchronously.
Hello,
The same problem with all
responType !== 'json'whether it istext,bloborarraybuffer.For example:
In my case I’ve a
401 Unthorizedas response status, the response body will be casted to the requestedresponseType(anArrayBeffer) so myRefreshTokeninterceptor is wating for an object (json) but got anarrayBefferinstead.As a workaround in my interceptor:
which is the same workaround proposed above but for an
arraybuffer, and we shoud do the same fortext.I think in case of an error, angular should only try to parse it as a json regardless of the requested
responseType.You can read blob as text and then convert it to json.