Closed wisonlau closed 2 months ago
// SetHeaderMap sets custom HTTP headers with map. func (c Client) SetHeaderMap(m map[string]string) Client { for k, v := range m { c.header[k] = v } return c }
官方库里面的这个方法能不能修改,或者新增一个方法,m map[string]string -> m map[string][]string
暂不支持自定义设置,可以用标准库可以解决
// 创建一个新的 HTTP 请求
req, err := http.NewRequest("POST", "http://httpbin.org/post", bytes.NewBuffer([]byte(`{"key":"value"}`)))
if err != nil {
fmt.Println("Error creating request:", err)
return
}
req.Header["user-agent"] = []string{"my-custom-client"}
Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑🤝🧑👫🧑🏿🤝🧑🏻👩🏾🤝👨🏿👬🏿
Custom settings are not supported yet and can be solved using the standard library.
// Create a new HTTP request
req, err := http.NewRequest("POST", "http://httpbin.org/post", bytes.NewBuffer([]byte(`{"key":"value"}`)))
if err != nil {
fmt.Println("Error creating request:", err)
return
}
req.Header["user-agent"] = []string{"my-custom-client"}
@wisonlau @hailaz 大家好,
-
连接多个单词。goframe
的gclient
包中,是通过标准库的HTTP Header Set
方法来设置的Header
,具体源码在这里 https://github.com/gogf/gf/blob/183395f0a0f59c1680ce0e78638dd541e4e1c84e/net/gclient/gclient_request.go#L329Header
,那么可以考虑使用gclient
的前置中间件来实现,具体请参考文档 https://goframe.org/pages/viewpage.action?pageId=7301625
package main
import ( "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/net/gclient" "net/http" )
func main() { c := g.Client() c.Use(func(c gclient.Client, r http.Request) (resp *gclient.Response, err error) { // 注意这里不要使用r.Header.Set方法设置Header,会被标准化转换为规范的Header Key r.Header["userid"] = []string{"10000"} resp, err = c.Next(r) return resp, err }) }
Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑🤝🧑👫🧑🏿🤝🧑🏻👩🏾🤝👨🏿👬🏿
@wisonlau @hailaz Hello everyone,
- First of all, in the HTTP protocol, the standardized Header Key is capitalized, and the English word connection symbol
-
is used to connect multiple words.- Secondly, in the
gclient
package ofgoframe
, theHeader
is set through theHTTP Header Set
method of the standard library. The specific source code is here https://github.com/gogf/gf/blob/ 183395f0a0f59c1680ce0e78638dd541e4e1c84e/net/gclient/gclient_request.go#L329- Finally, if you really want to customize a unique
Header
, you can consider using thegclient
front-end middleware to implement it. For details, please refer to the document https://goframe.org/pages/viewpage.action ?pageId=7301625- Also, let me give you an example:
package main
import ( "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/net/gclient" "net/http" )
func main() { c := g.Client() c.Use(func(c gclient.Client, r http.Request) (resp *gclient.Response, err error) { // Be careful not to use the r.Header.Set method to set the Header here. It will be standardized and converted into a standard Header Key. r.Header["userid"] = []string{"10000"} resp, err = c.Next(r) return resp, err }) }
Go version
go version go1.23.0 darwin/amd64
GoFrame version
v2.7.2
Can this bug be reproduced with the latest release?
Option Yes
What did you do?
What did you see happen?
+---------------------------------------------+ | REQUEST | +---------------------------------------------+ POST /apiServer/v1/sms/sendMessageOne HTTP/1.1 Host: factor.dcdun.com User-Agent: GClient v2.7.2 at wisonlaudeMacBook-Pro.local Content-Length: 141 Content-Type: application/json Sign: 3317557c2b5bac9c9e08d54a739481e0 Timestamp: 1725549656 Traceparent: 00-a0e2b2404562f217bb0c8811ecc05b71-fd3756d6f43405f6-01 Userid: 46569 Accept-Encoding: gzip
{"messageList":[{"content":"【xxx】验证码808207,您正在进行身份验证,不要告诉别人哦!","phone":"123456"}]}
+---------------------------------------------+ | RESPONSE | +---------------------------------------------+ HTTP/1.1 200 Connection: close Transfer-Encoding: chunked Cache-Control: no-cache, no-store, max-age=0, must-revalidate Content-Type: application/json Date: Thu, 05 Sep 2024 15:20:56 GMT Eagleid: 716c4b2c17255496569171587e Expires: 0 Pragma: no-cache Server: Tengine Set-Cookie: sl-session=vCR3TNgd22YylisTR4Mlzw==; Path=/; Max-Age=86400; HttpOnly Timing-Allow-Origin: * Vary: Origin Vary: Access-Control-Request-Method Vary: Access-Control-Request-Headers Via: ens-cache5.l2em21-5[18,0], cache24.cn4594[44,0] X-Content-Type-Options: nosniff X-Xss-Protection: 0
{"code":500,"msg":"requestHeader中timestamp已过期"}
What did you expect to see?
有些第三方网站只支持头部首字母小写的参数,goframe的工具会把我的参数名字转成首字母大写,比如:timestamp这个参数,这个有什么临时的解决方案救急.