发送 HTTP/2 请求

Send HTTP/2 request

我尝试从 Go 发送 HTTP/2 请求,但我不能。

client := &net.Client{}
request, err := net.NewRequest("GET", someUrl, nil)
if err != nil {
    return "", err
}

// some headers
// some cookies

client.Transport = &http2.Transport{}

response, err := client.Do(request)

如果我打印转储请求

dr, _ := httputil.DumpRequest(request, false)
fmt.Println(string(dr))

那么结果如下

GET /some/url HTTP/1.1
// some headers
// some cookies

为什么?

I'm try send HTTP/2 request from Go, but I can't. ...

dr, _ := httputil.DumpRequest(request, false)
fmt.Println(string(dr))

Then it turns out the following

GET /some/url HTTP/1.1
// some headers
// some cookies

DumpRequest 不会打印在线路上发送的请求,特别不适合检查是否使用 HTTP/2。 documentation 明确指出:

DumpRequest returns the given request in its HTTP/1.x wire representation. It should only be used by servers to debug client requests. The returned representation is an approximation only; some details of the initial request are lost while parsing it into an http.Request. In particular, the order and case of header field names are lost. The order of values in multi-valued headers is kept intact. HTTP/2 requests are dumped in HTTP/1.x form, not in their original binary representations."