Golang:解释 DumpRequest 和 DumpResponse HTTP/2

Golang: Explain DumpRequest and DumpResponse HTTP/2

clientt := &http.Client{
    Timeout: 30 * time.Second,
}
var tr = &http2.Transport{}
clientt.Transport = tr

我创建了一个客户端并发送了 http/2 请求。使用 http2 传输 但在 DumpRequest 中我看到 获取/HTTP/1.1 主持人:www.xxxxq23.com

在响应转储中我看到 HTTP/2.0

为什么要求使用 HTTP/1.1? 如何改成HTTP/2.0

HTTP/2 是二进制的,而不是文本的,转储为二进制将是不可读和无用的。这是有意设计的,很好 documented:

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.

If body is true, DumpRequest also returns the body. To do so, it consumes req.Body and then replaces it with a new io.ReadCloser that yields the same bytes. If DumpRequest returns an error, the state of req is undefined.

您可以查看实施细节here