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: consider implementing browser http client at http.Client level instead of http.RoundTripper #25695
Comments
I think we'll need to think more about this and make some decisions. It's a fact that not all of the standard library functionality can be implemented in browsers, because in general browsers expose relatively high level APIs and not low level ones. As an example, the conn, err := net.Dial("tcp", "golang.org:80") It's clear that it can't work. But in what way would it not work? Viable choices are for This becomes trickier when you're dealing with a package like resp, err := http.Get("https://example.com/") But we know the HTTP server can't be implemented inside the browser, so this Go code wouldn't work (either it always returns a non-nil error, or fails to compile because err := http.ListenAndServe(":8080", nil) The question that comes up is what to do when the Go code tries to use an |
I've recently realized an important data point regarding exactly this. Right now, the For example, here's Go code I wrote that works on frontend (via GopherJS) for creating an import "golang.org/x/oauth2"
// httpClient gives an *http.Client for making API requests.
func httpClient() *http.Client {
accessToken, err := ... // get it from a Cookie, if set
if err != nil {
// Non-authenticated client.
return http.DefaultClient
}
// Authenticated client.
src := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: accessToken},
)
return oauth2.NewClient(context.Background(), src)
} It would indeed nice to have code that like continue to work under Would it be a good or bad idea to have a |
The motivation for considering implementing the HTTP client at
http.Client
level rather thanhttp.RoundTripper
, it has to do with the fact that browser APIs (such as XmlHTTPRequest and Fetch) do not allow making individual HTTP requests without interpreting the response code, they operate at a higher level, closer to whathttp.Client.Do
method does.This issue is a continuation of a discussion in CL 114515. /cc @bradfitz @johanbrandhorst @neelance
I wrote:
@johanbrandhorst wrote:
@bradfitz wrote:
I've been thinking about this more, and I have 2 comments (to be posted as replies). It's not clear to me what will end up being the best (least bad) solution.
The text was updated successfully, but these errors were encountered: