disabling HTTP/2 client with DefaultTransport

1,132 views
Skip to first unread message

JeffG

unread,
Sep 6, 2022, 8:33:03 AM9/6/22
to golang-nuts
Hi, 

I'm trying to understand why in the following code,  client2 doesn't work 'http2_handshake_failed" error)

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)
    }
}
...

Dennis Vashchuk

unread,
Oct 18, 2022, 6:04:01 PM10/18/22
to golang-nuts
```
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,
}
```

Jean-François Giorgi

unread,
Oct 18, 2022, 9:06:23 PM10/18/22
to Dennis Vashchuk, golang-nuts
Hi

I don't understand your reply: that is what I do in client1 already...
my issue was 'disabling HTTP/2 client with DefaultTransport'.

--
You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/atm7brPdXac/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts...@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 all
Reply to author
Forward
0 new messages