req
req copied to clipboard
关于 SetHeadersNonCanonical(协议头大小写敏感) 在 https 中失效
trafficstars
func TestHttps(t *testing.T) {
c := req.C().EnableDumpAll().SetBaseURL("https://httpbin.org")
r := c.R()
r.SetHeadersNonCanonical(map[string]string{
"USID": "123", // 用户 ID
})
r.SetHeaderNonCanonical("CTERMINAL", "ui")
res, _ := r.Post("/post")
t.Log(res.String())
}
/*
:authority: httpbin.org
:method: POST
:path: /post
:scheme: https
usid: 123
cterminal: ios
content-length: 0
accept-encoding: gzip
user-agent: req/v3 (https://github.com/imroc/req)
*/
func TestHttp(t *testing.T) {
c := req.C().EnableDumpAll().SetBaseURL("http://httpbin.org")
r := c.R()
r.SetHeadersNonCanonical(map[string]string{
"USID": "123", // 用户 ID
})
r.SetHeaderNonCanonical("CTERMINAL", "ui")
res, _ := r.Post("/post")
t.Log(res.String())
}
/*
POST /post HTTP/1.1
Host: httpbin.org
User-Agent: req/v3 (https://github.com/imroc/req)
Content-Length: 0
CTERMINAL: ui
USID: 123
Accept-Encoding: gzip
*/
那是因为HTTPS自动嗅探服务端所支持的协议(http1/2/3),选择到了http2,而http2和http3都固定只能是小写,没有大写一说。只有http1才有大小写。如果需要,你可以强制使用http1:
c := req.C().EnableDumpAll().SetBaseURL("https://httpbin.org").EnableForceHTTP1()
参考注释说明:
// SetHeadersNonCanonical set headers from a map for the request which key is a
// non-canonical key (keep case unchanged), only valid for HTTP/1.1.
func (r *Request) SetHeadersNonCanonical(hdrs map[string]string) *Request {