```
transport := &http.Transport{
TLSClientConfig: &tls.Config{},
TLSNextProto: make(map[string]func(authority string, c *tls.Conn) 
http.RoundTripper), // Disable HTTP/2
}

client := &http.Client{
Transport: transport,
}
```

On Tuesday, September 6, 2022 at 3:33:03 PM UTC+3 JeffG wrote:

> Hi, 
>
> I'm trying to understand why in the following code,  client2 doesn't work 
> 'http2_handshake_failed" error)
> According from 
> https://pkg.go.dev/net/http#pkg-overview:~:text=Starting%20with%20Go,are%20currently%20supported%3A
>  
> ,it's the way to disable HTTP/2.
>
> thx
> ...
> package main
>
> import (
>     "crypto/tls"
>     "fmt"
>     "net/http"
>     "time"
> )
>
> func main() {
>
>     // client1, disable HTTP/2
>     httpClient1 := &http.Client{
>         Transport: &http.Transport{
>             TLSNextProto: map[string]func(string, *tls.Conn) 
> http.RoundTripper{},
>         },
>     }
>
>     // client2, clone default transport, disable HTTP/2
>     var netTransport = http.DefaultTransport.(*http.Transport).Clone()
>     netTransport.TLSNextProto = map[string]func(string, *tls.Conn) 
> http.RoundTripper{}
>     netTransport.ForceAttemptHTTP2 = false
>
>     httpClient2 := &http.Client{
>         Transport: netTransport,
>     }
>
>     doClient(httpClient1)
>     doClient(httpClient2)
> }
>
> func doClient(client *http.Client) {
>     resp, err := client.Get("https://google.com/";)
>     if err != nil {
>         fmt.Printf("client error : %s\n", err)
>     } else {
>         defer resp.Body.Close()
>         fmt.Printf("Response status: %s with protocol %s\n", resp.Status, 
> resp.Proto)
>     }
> }
> ...
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/b4b83ec1-ef6a-4cf6-9f13-2dd973464a2fn%40googlegroups.com.

Reply via email to