angular: Successful http responses (e.g. 201) returned as errors

I’m submitting a…


[X] 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

Current behavior

HttpClient observables returns 201 (and probably other 20X except 200) http status as error.

Expected behavior

Status < 300 should be returned as next, not error.

Minimal reproduction of the problem with instructions

I can prepare a plunker if desired (or unable to reproduce) - tell me if needed.

What is the motivation / use case for changing the behavior?

This is a regression between 4.3 -> 4.4. In 4.3.6, it is correct, in 4.4.4, it is not. It may be related to another 4.3 -> 4.4 HttpClient regression: https://github.com/angular/angular/issues/19103.

Environment


Angular version: 4.4.4


Browser:
- [X] Chrome (desktop) version 61
 
For Tooling issues:
- Node version: 6.11.3  
- Platform:  Linux

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 17
  • Comments: 24 (4 by maintainers)

Commits related to this issue

Most upvoted comments

I don’t think that’s a bug per se: the httpclient by default expects some json in the body, if you don’t want that, then use the responseType property in the http method option (see for example: https://github.com/angular/angular/issues/18680#issuecomment-330425866).

I had the same problem, but I realized that my back-end was returning wrong JSON format.

I was also having the same issue, my below code: public mymethod(data: myObject): Observable<any> { const url: string = 'myURL'; const headers = new Headers(); headers.append('Content-Type', 'application/json'); const options = new RequestOptions({ headers: headers }); return this._http.post(url, data, options); .map(this.extractDataJSON) .catch(this.handleErrorObservable); } }

I was handling my data using .map and .catch methods. I removed both. my issue is solved.

Same error here… There are any updates on it?

@victornoel No, 4.4.6 is still broken, since you can either pass responseType: string (but then, you don’t get json parsed on error), or responseType: json (but then, you don’t get “next” on 201 successful response).

Another argument for the more liberal behavior is the following case: server returns successful response empty, but error response with a json. Now, the only option is to use responseType: text and manually parse error response. I’d rather use responseType: json, let angular parse the error response, and get null as body.

FIXED on BACKEND

In my case:

back-end which is written on C# hadn’t been enable CORS:

config.EnableCors(); and [EnableCors(origins: "*", headers: "*", methods: "*")] import from using System.Web.Http.Cors;

Duplicate of #18680 and #19413.

@victornoel Thank you, adding responseType: “blob” fixed the problem for me. Anyway, it is a bit counterintuitive, I would expect to get null for responseType: “json” and no or empty response. Yes, the content-type: application/json header is missing in the response headers, so strictly speaking, angular is correct, but I guess it is better to be slightly incorrect in this case.