发送HTTP/2请求

huangapple go评论93阅读模式
英文:

Send HTTP/2 request

问题

我正在尝试从Go发送HTTP/2请求,但是遇到了问题。

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

// 一些头部信息
// 一些cookie

client.Transport = &http2.Transport{}

response, err := client.Do(request)

如果我打印请求的dump信息

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

然后输出如下

GET /some/url HTTP/1.1
// 一些头部信息
// 一些cookie

为什么会这样呢?

英文:

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

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)

If I print dump request

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

Why?

答案1

得分: 2

我试图从Go发送HTTP/2请求,但是无法成功。...

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

然后结果如下:

GET /some/url HTTP/1.1
// 一些头部信息
// 一些Cookie

DumpRequest不会将请求打印为发送到网络的形式,而且特别不适合检查是否使用了HTTP/2。文档明确说明:

DumpRequest以其HTTP/1.x的网络表示形式返回给定的请求。它只应由服务器用于调试客户端请求。返回的表示仅是一个近似值;在将其解析为http.Request时,会丢失一些初始请求的细节。特别是,丢失了头字段名称的顺序和大小写。多值头部中的值的顺序保持不变。HTTP/2请求以HTTP/1.x的形式转储,而不是以其原始二进制表示形式转储。

英文:

> 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 will not print the request as sent on the wire and is specifically not suitable for checking if HTTP/2 is used. The documentation explicitly states:

> 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."

huangapple
  • 本文由 发表于 2021年11月18日 19:02:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/70018694.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定