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

3

5

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?

Klemens answered 26/12, 2015 at 3:23 Comment(0)
G
10

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

Gemmagemmate answered 26/12, 2015 at 3:37 Comment(0)
S
3

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))
Sliwa answered 30/12, 2023 at 1:36 Comment(0)
H
-1

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.

Hyacinthia answered 2/3, 2016 at 6:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.