Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

关于 SetHeadersNonCanonical(协议头大小写敏感) 在 https 中失效 #374

Open
0xObjc opened this issue Jul 21, 2024 · 2 comments
Open

Comments

@0xObjc
Copy link

0xObjc commented Jul 21, 2024

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
*/
@imroc
Copy link
Owner

imroc commented Jul 22, 2024

那是因为HTTPS自动嗅探服务端所支持的协议(http1/2/3),选择到了http2,而http2和http3都固定只能是小写,没有大写一说。只有http1才有大小写。如果需要,你可以强制使用http1:

	c := req.C().EnableDumpAll().SetBaseURL("https://httpbin.org").EnableForceHTTP1()

@imroc
Copy link
Owner

imroc commented Jul 22, 2024

参考注释说明:

// 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 {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants