LoginSignup
19
16

More than 5 years have passed since last update.

go 1.6でhttp.Clientが自動的にhttp/2を使わないようにする方法

Posted at

(この記事は投稿時点でのgotip版に基づいています。go 1.6リリース時には変更される可能性があります)

Go 1.6では、通常のhttp.Clientを使ってhttpsリクエストを行うと、自動的にhttp2にアップグレードされます。大変便利ですが、検証時など、これを無効にしたいこともあります。そんなときは、次のいずれかの方法を使ってアップグレードを抑止することができます。

環境変数GODEBUGを使う

GODEBUG=h2client=0

http.TransportのTLSNextProtoを非nilにする

        // TLSNextProto specifies how the Transport switches to an
        // alternate protocol (such as HTTP/2) after a TLS NPN/ALPN
        // protocol negotiation.  If Transport dials an TLS connection
        // with a non-empty protocol name and TLSNextProto contains a
        // map entry for that key (such as "h2"), then the func is
        // called with the request's authority (such as "example.com"
        // or "example.com:1234") and the TLS connection. The function
        // must return a RoundTripper that then handles the request.
        // If TLSNextProto is nil, HTTP/2 support is enabled automatically.
        TLSNextProto map[string]func(authority string, c *tls.Conn) RoundTripper
http.DefaultTransport.(*http.Transport).TLSNextProto = map[string]func(authority string, c *tls.Conn) http.RoundTripper{}

以上です。

19
16
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
19
16