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

proposal: net/http: add new Context taking/suffixed helper functions for {Get, Head, Post, PostForm} #35562

Open
rittneje opened this issue Nov 13, 2019 · 5 comments
Labels
Projects
Milestone

Comments

@rittneje
Copy link

@rittneje rittneje commented Nov 13, 2019

The documentation for Client.Do says "Generally Get, Post, or PostForm will be used instead of Do." However, there is no way to pass a context.Context into those methods, so we cannot, for example, set a per-request timeout without resorting to Do. I propose adding the following new methods to http.Client.

  • GetContext(ctx context.Context, url string) (resp *Response, err error)
  • HeadContext(ctx context.Context, url string) (resp *Response, err error)
  • PostContext(ctx context.Context, url, contentType string, body io.Reader) (resp *Response, err error)
  • PostFormContext(ctx context.Context, url string, data url.Values) (resp *Response, err error)

This would effectively deprecate the existing Get, Head, Post, and PostForm methods.

We should also make a corresponding global function for each of these, and deprecate the existing http.Get, http.Head, http.Post, and http.PostForm functions.

@gopherbot gopherbot added this to the Proposal milestone Nov 13, 2019
@gopherbot gopherbot added the Proposal label Nov 13, 2019
@zikaeroh

This comment has been minimized.

Copy link

@zikaeroh zikaeroh commented Nov 13, 2019

This doesn't resolve the proposal, but you might not be aware of ctxhttp in x/net: https://godoc.org/golang.org/x/net/context/ctxhttp

@odeke-em

This comment has been minimized.

Copy link
Member

@odeke-em odeke-em commented Nov 13, 2019

Hello @rittneje! Thank you for filing this issue. I see how this is inconvenient. However, history beat us to the punch, those functions were added years before context.Context was a thing. I am sympathetic towards adding Context on them. On the flip side though:

The highlighted functions are helper/convenience methods on the global/default HTTPClient.
To customize more things on those methods, we recommend taking a look at NewRequest et al for example https://golang.org/pkg/net/http/#Post
Screen Shot 2019-11-13 at 11 05 28 AM

The already existing customizers are:

but perhaps we should add examples enumerating how to make the various requests corresponding to those highlighted functions showing how to accomplish them but with customization and NewRequestWithContext, but also add the notice to use NewRequestWithContext for better customization.

Adding more helper functions with a new flavor would be increasing the API surface for things that we already recommend alternatives to.

/cc @bradfitz

@odeke-em odeke-em changed the title Proposal: net/http: add Context methods to http.Client proposal: net/http: add new Context suffixed helper functions for {Get, Head, Post, PostForm} Nov 13, 2019
@odeke-em odeke-em changed the title proposal: net/http: add new Context suffixed helper functions for {Get, Head, Post, PostForm} proposal: net/http: add new Context taking/suffixed helper functions for {Get, Head, Post, PostForm} Nov 13, 2019
@rittneje

This comment has been minimized.

Copy link
Author

@rittneje rittneje commented Nov 18, 2019

@odeke-em It does kind of stink that there's no real convenience method for issuing a GET request anymore without dropping the context on the floor. I don't really consider that to be a customization at all, since dropping context is a code smell. In any case, if you do decide not to add the missing helpers, you should probably remove that statement from the documentation of Client.Do since it is no longer true.

@rsc rsc added this to Incoming in Proposals Nov 27, 2019
@rsc

This comment has been minimized.

Copy link
Contributor

@rsc rsc commented Dec 4, 2019

I was writing some HTTP request code earlier today and I had to use http.NewRequest and client.Do because I had to set some headers. It's not a huge deal to have to do that. The convenience functions don't have to provide for every possible convenience, or else they stop being convenient.

@rittneje

This comment has been minimized.

Copy link
Author

@rittneje rittneje commented Dec 4, 2019

@rsc I agree they don't have to provide for every possible convenience, but the point is that the old convenience methods really should never be used anymore outside toy example code. So either new versions should be added to accept a context (to not encourage developers to drop the incoming context), or the comment on Client.Do should be revised.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Proposals
Incoming
5 participants
You can’t perform that action at this time.