Skip to content

Deprecate or analyzer-warn HttpClient IsSuccessStatusCode and EnsureSuccessStatusCode #113177

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
scalablecory opened this issue Mar 5, 2025 · 7 comments

Comments

@scalablecory
Copy link
Contributor

scalablecory commented Mar 5, 2025

HttpResponseMessage.IsSuccessStatusCode and HttpResponseMessage.EnsureSuccessStatusCode are anti-patterns:

These encourage non-defensive design: if an API returns 200 OK every day, and one day returns 202 Accepted -- is my app doing the correct thing by assuming both are an equivalent result?

I see these used fairly often, and it's usually the first thing newbies reach for.
.NET should warn people away from these and encourage testing for explicit status codes.

@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Mar 5, 2025
Copy link
Contributor

Tagging subscribers to this area: @dotnet/ncl
See info in area-owners.md if you want to be subscribed.

@julealgon
Copy link

These are on HttpResponseMessage, not on HttpClient.

I don't quite agree that IsSuccessStatusCode is "an anti-pattern", after all it's just a boolean that you can use to check if something is in the 2XX range, which by itself is not wrong.

I could see an argument for EnsureSuccessStatusCode being more problematic, but maybe not necessarily for the same reason as you see it: it promotes using exceptions as control flow which is a bad practice (and slow).

Having said all that, the proper "recommendation" should be "use kiota and generate a proxy from a well-defined open api spec that has the status codes properly mapped".

@scalablecory
Copy link
Contributor Author

I literally can not remember a time I've seen someone use these in a way that was correct. They are an anti-pattern.

@MihaZupan
Copy link
Member

I see these as simple helpers that are often useful for ruling out responses that you know definitely aren't okay. They don't prevent you from doing further validation.
At least in cases where I've used it, I don't care which exact status code was returned - if something did change, I expect deserializing the response content to fail and surface that.

Helper functions like HttpClient.GetStringAsync, GetFromJsonAsync, etc. are worse from this perspective as they do hide information about which status code was returned (they just call EnsureSuccessStatusCode), and yet I don't see us ever obsoleting them.

@scalablecory
Copy link
Contributor Author

Filed somewhat out of annoyance; I had just fixed a bug hidden by use of this, when an API started returning OK instead of Created.

This isn't the first time I've made code more robust by removing use of this API -- I'd agree those other shortcuts are bad for the same reason.

I understand it's liked because it's simple, and that'll make it hard to want to do anything about it. But that's part of why I consider them so bad. These are so tempting to use, but are usually a bad choice.

@MihaZupan
Copy link
Member

MihaZupan commented Mar 11, 2025

The team consensus here is that we don't see a need to obsolete these helpers as they don't prevent the user from doing stricter validation, and are often sufficient.

But we do see value in improving our documentation to:

  • Call out where we are using these checks ourselves and you can't control it, i.e. all HttpClient.Get{String/ByteArray/Stream/FromJson}Async helpers - HttpClient.GetStreamAsync raising HttpRequestException dotnet-api-docs#9148.
  • Include a note along the lines of "consider checking that HttpResponseMessage.StatusCode exactly matches the expected value instead".
  • Consider improving indicates if the HTTP response was successful to include its definition of "successful". it already does in Returns: section.

@MihaZupan MihaZupan closed this as not planned Won't fix, can't repro, duplicate, stale Mar 11, 2025
@dotnet-policy-service dotnet-policy-service bot removed the untriaged New issue has not been triaged by the area owner label Mar 11, 2025
@MihaZupan
Copy link
Member

MihaZupan commented Mar 11, 2025

This is also something where BannedApiAnalyzers could help within your org.

P:System.Net.Http.HttpResponseMessage.IsSuccessStatusCode
M:System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode

@github-actions github-actions bot locked and limited conversation to collaborators Apr 10, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants