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

frame/g: issue g.client will change the header parameters #3758

Closed
wisonlau opened this issue Sep 5, 2024 · 5 comments
Closed

frame/g: issue g.client will change the header parameters #3758

wisonlau opened this issue Sep 5, 2024 · 5 comments
Labels
bug It is confirmed a bug, but don't worry, we'll handle it.

Comments

@wisonlau
Copy link

wisonlau commented Sep 5, 2024

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?

header := map[string]string{
	"userid":    gconv.String(platform["userId"]),
	"timestamp": nowTime,
	"sign":      md5Sign,
}
data, pErr := g.Client().ContentJson().SetHeaderMap(header).Post(
	ctx,
	platform["url"].(string),
	postData,
)
if pErr != nil {
	panic(pErr)
}
defer data.Close()
data.RawDump()

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这个参数,这个有什么临时的解决方案救急.

@wisonlau wisonlau added the bug It is confirmed a bug, but don't worry, we'll handle it. label Sep 5, 2024
@wisonlau wisonlau changed the title frame/g: issue title frame/g: issue g.client will change the header parameters Sep 5, 2024
@wisonlau
Copy link
Author

wisonlau commented Sep 6, 2024

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

@hailaz
Copy link
Member

hailaz commented Sep 6, 2024

暂不支持自定义设置,可以用标准库可以解决

// 创建一个新的 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"}

@hailaz hailaz closed this as completed Sep 6, 2024
@Issues-translate-bot
Copy link

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

@gqcn
Copy link
Member

gqcn commented Oct 5, 2024

@wisonlau @hailaz 大家好,

  • 首先,在HTTP协议中,规范化的Header Key是首字母大写的,并且使用英文词连接符号-连接多个单词。
  • 其次,在goframegclient包中,是通过标准库的HTTP Header Set方法来设置的Header,具体源码在这里
    req.Header.Set(k, v)
  • 最后,如果实在是想要自定义与众不同的Header,那么可以考虑使用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
	})
}

@Issues-translate-bot
Copy link

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 of goframe, the Header is set through the HTTP Header Set method of the standard library. The specific source code is here https://github.com/gogf/gf/blob/ 183395f/net/gclient/gclient_request.go#L329
  • Finally, if you really want to customize a unique Header, you can consider using the gclient 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
})
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug It is confirmed a bug, but don't worry, we'll handle it.
Projects
None yet
Development

No branches or pull requests

4 participants