Matt Joiner
Matt Joiner

Reputation: 118470

How do I make a HTTP2-only request over http:// in Go?

I'm using Go 1.6 and want to make a HTTP2-only request over http://.

Attempting to do this currently results in:

Head http://localhost:2076/completed/764c1b6bc55548707507a2dd25570483a7216bf4: http2: unsupported scheme

To force http2, I believe I need http.Client.Transport.TLSConfig.NextProtos set to []string{"h2"}.

What else is required?

Upvotes: 5

Views: 3573

Answers (3)

k_o_
k_o_

Reputation: 6288

HTTP2 can be enforced by using http2.Transport.AllowHTTP. Also the answer to use https is not valid anymore in 2023. HTTP2 does not require always TLS (although web browsers require it, client server apps can use plain connections.)

import (
    "golang.org/x/net/http2"
    "net/http"
)

...

    client := &http.Client{
        Transport: &http2.Transport{
            AllowHTTP:      true,
            DialTLSContext: nil,
            DialTLS: func(network, addr string, cfg *tls.Config) (net.Conn, error) {
                return net.Dial(network, addr)
            },
        },
    }
...

    h2server := &http2.Server{IdleTimeout: time.Second * 60}
    h2handler := h2c.NewHandler(http.HandlerFunc(handler), h2server)
    log.Fatal(http.ListenAndServe("127.0.0.1:1234", h2handler))

Upvotes: 3

Dhumil Agarwal
Dhumil Agarwal

Reputation: 874

The HTTP/2.0 by default works on highly secured connections. It uses high quality ciphers. So it can only run on HTTPS connections. Moreover, to make HTTPS connections, you also need to have your SSL enabled and have the required certificates installed.

Upvotes: -1

hobbs
hobbs

Reputation: 239712

You need to use https, not http. The http2 transport doesn't recognize the http scheme.

Upvotes: 10

Related Questions