angular: HttpClient should allow different responseTypes for success response and error response
I’m submitting a…
[ ] Regression (a behavior that used to work and stopped working in a new release)
[ ] Bug report
[ x ] 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
Current behavior
Currently, HttpClient expects the same responseType for both, success responses as well as error responses. This brings up issues when a WEB API returns e. g. JSON but just an (non JSON based) error string in the case of an error.
Expected behavior
In see two solutions: Provide an own errorResponseType field or assign the received message as a string when JSON parsing does not work (as a fallback)
Minimal reproduction of the problem with instructions
The following test case is using such an Web API. It returns json for the success case and plain text in the case of an error. Both tests lead to an 400 error due to validation issues. In the first case where we are requesting plain text, the text based error message is shown; in the second case where we are requesting json (which would be the case if the call succeeded) we are just getting null.
import { TestBed } from '@angular/core/testing';
import { HttpClient, HttpClientModule, HttpErrorResponse, HttpHeaders } from '@angular/common/http';
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
fdescribe('Different responseTypes for success and error case', () => {
let http: HttpTestingController;
let httpClient: HttpClient;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [ HttpClientModule ]
});
httpClient = TestBed.get(HttpClient);
});
it('should send an HttpErrorResponse where the error property is the received body', (complete: Function) => {
let actualBody;
httpClient.post('http://www.angular.at/api/flight', {}, { responseType: 'text' }).subscribe(null, (resp: HttpErrorResponse) => {
actualBody = resp.error;
expect(actualBody).not.toBeNull();
complete();
});
});
it('should send an HttpErrorResponse where the error property is the body of the flushed response', (complete: Function) => {
let actualBody;
httpClient.post('http://www.angular.at/api/flight', {}, { responseType: 'json' }).subscribe(null, (resp: HttpErrorResponse) => {
actualBody = resp.error;
expect(actualBody).not.toBeNull();
complete();
});
});
});
What is the motivation / use case for changing the behavior?
Using existing Web APIs more seamlessly
Environment
Angular version: 4.3
For Tooling issues:
- Platform: Windows
About this issue
- Original URL
- State: open
- Created 7 years ago
- Reactions: 53
- Comments: 20 (3 by maintainers)
A year later with Angular 6 still an issue. In my opinion this is a bug, not a feature.
Hello in 2023 😀 With Angular 15. Still the same stuff
Hello in 2024… Angular 17… still having this issue. 😅
My workaround is to add an interceptor with:
Hello in 2022. This is still a problem, 5 years later…
@trotyl See this example. Spring
@RestController
handler method.This returns a
Blob
object, basically. Then, the AngularHttpClient
part.This works if the request is successful. However, in case of exception thrown from the Spring handler method, the body is set to
Blob
.The Spring
ExceptionHandler
is pretty standard, and should produce aJSON
body.Hey peps ✌️ I also ran into the same issue yesterday.
My app is requesting a ZIP from the API, but receives JSON in case of an error. I was wondering if
responseType
could be conditional based on theContent-Type
of the response.The requesting code would look something like this:
For me, I use a synchronous way to convert Blob to json. I wrote a post about it here: https://stackoverflow.com/questions/48500822/how-to-handle-error-for-response-type-blob-in-httprequest/70977054#70977054
This is how it works:
The parsed
errorJson
in the error clause will now contain{error-msg: 'get call failed'}
.