Skip to content
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

net/http StatusCode and 404s #21846

Closed
ror6ax opened this issue Sep 12, 2017 · 5 comments
Closed

net/http StatusCode and 404s #21846

ror6ax opened this issue Sep 12, 2017 · 5 comments

Comments

@ror6ax
Copy link

ror6ax commented Sep 12, 2017

I'd argue that resp, err := http.Get("http://google.com/foo/bar") not having any err's but resp.StatusCode indicating 404 is misleading.

If 404 and 500 are not errors, what are? Why are there 2 types of errors?

@davecheney
Copy link
Contributor

davecheney commented Sep 12, 2017 via email

@bradfitz
Copy link
Contributor

I'm pretty sure this is already documented. (on phone)

@mvdan
Copy link
Member

mvdan commented Sep 12, 2017

The documentation:

An error is returned if caused by client policy (such as CheckRedirect), or failure to speak HTTP (such as a network connectivity problem). A non-2xx status code doesn't cause an error.

If the returned error is nil, the Response will contain a non-nil Body which the user is expected to close. If the Body is not closed, the Client's underlying RoundTripper (typically Transport) may not be able to re-use a persistent TCP connection to the server for a subsequent "keep-alive" request.

Also note that, on top of what @davecheney said, making 4xx and 5xx status codes also errors would break the Go1 compatibility promise.

@ror6ax
Copy link
Author

ror6ax commented Sep 12, 2017

Is there a catchall non-error return code object to match against? I mean, having != 200,201,301,302 etc etc is a lot of boilerplate to simply make sure my request has made it.
Python's requests lib would error out on 4**/5** and just run happily on everything else, I'd sort of used to such behavior.

@mvdan
Copy link
Member

mvdan commented Sep 12, 2017

code >= 400 is what I tend to use.

There doesn'r seem to be a bug here, so I'm closing this. If you'd like to change how net/http works, feel free to submit a proposal.

@mvdan mvdan closed this as completed Sep 12, 2017
@golang golang locked and limited conversation to collaborators Sep 12, 2018
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

5 participants