Go语言 检查HTTP请求是否为HTTP2?

nc1teljy  于 2023-11-14  发布在  Go
关注(0)|答案(1)|浏览(131)

如何检查连接到HTTP服务器是HTTP1.1还是HTTP2?

func(w http.ResponseWriter, r *http.Request){
  ...
}

字符串

7z5jn7bk

7z5jn7bk1#

http.Request包含协议版本的字段:

type Request struct {
    // ...

    // The protocol version for incoming server requests.
    //
    // For client requests, these fields are ignored. The HTTP
    // client code always uses either HTTP/1.1 or HTTP/2.
    // See the docs on Transport for details.
    Proto      string // "HTTP/1.0"
    ProtoMajor int    // 1
    ProtoMinor int    // 0
    
    // ...
}

字符串
您可以简单地检查ProtoMajor字段是否为2

相关问题