Golang 错误的 http 请求 header
Golang wrong http resquest header
我正在构建 2 apis。一个向另一个提出请求。
要调用接收请求的api,我们需要传递一个X-Token Header。我正在使用 Golang
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
},
}
req, err := http.NewRequest("GET", "https://localhost:8086/v2/example", nil)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"Error": err.Error()})
}
req.Header.Add("accept", "application/json")
req.Header.Add("content-type", "application/json")
req.Header.Add("x-token", "a2e63ee01401aaeca78be023dfbb8c59")
resp, err := client.Do(req)
在另一个 API 中,我得到了带有 gin 的 http header,如下所示:
token := c.Request.Header.Get("x-token")
我不知道为什么我的 header 到达时有另一个值而没有 X-Token。谢谢!
fmt.Printf("%+v", c.Request.Header)
的结果:
map[User-Agent:[Go-http-client/1.1] Referer:[https://localhost:8086/v2/example] Accept-Encoding:[gzip]]
我不知道我的x-token、接受和[=52=在哪里]headers....
重要
- 如果我用 x-token header 在 Postman 上向所请求的 API 发出请求,我得到正确的 header .
- 如果我更改发出请求的API上的请求地址,例如httpbin,我也得到正确的header....
大家好!我找到了解决方案....
我还不知道为什么...但我认为 golang 不处理 没有尾部斜线 url 的....
https://localhost:8086/v2/example
不同于
https://localhost:8086/v2/example/
那是我的问题....
我只是复制并粘贴了邮递员的 golang 生成代码...这就是 "biggest" 不同之处....
谢谢先生。邮递员...
我正在构建 2 apis。一个向另一个提出请求。
要调用接收请求的api,我们需要传递一个X-Token Header。我正在使用 Golang
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
},
}
req, err := http.NewRequest("GET", "https://localhost:8086/v2/example", nil)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"Error": err.Error()})
}
req.Header.Add("accept", "application/json")
req.Header.Add("content-type", "application/json")
req.Header.Add("x-token", "a2e63ee01401aaeca78be023dfbb8c59")
resp, err := client.Do(req)
在另一个 API 中,我得到了带有 gin 的 http header,如下所示:
token := c.Request.Header.Get("x-token")
我不知道为什么我的 header 到达时有另一个值而没有 X-Token。谢谢!
fmt.Printf("%+v", c.Request.Header)
的结果:
map[User-Agent:[Go-http-client/1.1] Referer:[https://localhost:8086/v2/example] Accept-Encoding:[gzip]]
我不知道我的x-token、接受和[=52=在哪里]headers....
重要
- 如果我用 x-token header 在 Postman 上向所请求的 API 发出请求,我得到正确的 header .
- 如果我更改发出请求的API上的请求地址,例如httpbin,我也得到正确的header....
大家好!我找到了解决方案....
我还不知道为什么...但我认为 golang 不处理 没有尾部斜线 url 的....
https://localhost:8086/v2/example
不同于
https://localhost:8086/v2/example/
那是我的问题....
我只是复制并粘贴了邮递员的 golang 生成代码...这就是 "biggest" 不同之处....
谢谢先生。邮递员...