Golang: Explain DumpRequest and DumpResponse HTTP/2

980 Views Asked by At
clientt := &http.Client{
    Timeout: 30 * time.Second,
}
var tr = &http2.Transport{}
clientt.Transport = tr

I create a client and send http/2 request. with http2 transport but in DumpRequest I see GET / HTTP/1.1 Host: www.xxxxq23.com

In response dump I see HTTP/2.0

Why request use HTTP/1.1 ? How to change to HTTP/2.0

1

There are 1 best solutions below

0
On BEST ANSWER

HTTP/2 is binary, instead of textual and dumping in binary would be unreadable and useless. It is intentional by design and it is well 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.

You can checkout the implementation details here