如何在 golang net/http 中使用 Transport 添加 headers 信息
How to add headers info using Transport in golang net/http
我正在尝试通过创建 Trasport 来控制 keep-alives session 重用 tcp 连接。
这是我的代码片段,我不确定如何添加 headers 信息以进行身份验证。
url := "http://localhost:8181/api/v1/resource"
tr := &http.Transport{
DisableKeepAlives: false,
MaxIdleConns: 0,
MaxIdleConnsPerHost: 0,
IdleConnTimeout: time.Second * 10,
}
client := &http.Client{Transport: tr}
resp, err := client.Get(url)
不要混淆请求中的 Client。
客户端使用 Transport 和 运行 请求:client.Do(req)
您在 http.Request
with (h Header) Set(key, value string)
上设置了页眉:
req.Header.Set("name", "value")
这是我发现的:
package main
import (
"fmt"
"io/ioutil"
"net/http"
)
var URL = "http://httpbin.org/ip"
func main() {
tr := &http.Transport{DisableKeepAlives: false}
req, _ := http.NewRequest("GET", URL, nil)
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", "Token"))
req.Close = false
res, err := tr.RoundTrip(req)
if err != nil {
fmt.Println(err)
}
body, _ := ioutil.ReadAll(res.Body)
fmt.Println(string(body))
}
而且有效。
对于您的特定问题,这可能不是您想要的 - 在请求中设置它对您的情况更有意义,但要直接回答您的问题,您应该能够添加默认值 header通过为您的传输使用自定义 RoundTrip
方法通过传输的所有请求。
查看 https://golang.org/pkg/net/http/#RoundTripper
类似于:
type CustomTransport struct {
http.RoundTripper
}
func (ct *CustomTransport) RoundTrip(req *http.Request) (*http.Response, error) {
req.Header.Add("header-key", "header-value")
return ct.RoundTripper.RoundTrip(req)
}
url := "http://localhost:8181/api/v1/resource"
tr := &CustomTransport{
DisableKeepAlives: false,
MaxIdleConns: 0,
MaxIdleConnsPerHost: 0,
IdleConnTimeout: time.Second * 10,
}
client := &http.Client{Transport: tr}
resp, err := client.Get(url)
当我无法直接访问 API 客户端库使用的 http
客户端(或每个请求 object 直接访问)时,我发现这很有用,但它允许我来传递一个交通工具。
我正在尝试通过创建 Trasport 来控制 keep-alives session 重用 tcp 连接。
这是我的代码片段,我不确定如何添加 headers 信息以进行身份验证。
url := "http://localhost:8181/api/v1/resource"
tr := &http.Transport{
DisableKeepAlives: false,
MaxIdleConns: 0,
MaxIdleConnsPerHost: 0,
IdleConnTimeout: time.Second * 10,
}
client := &http.Client{Transport: tr}
resp, err := client.Get(url)
不要混淆请求中的 Client。
客户端使用 Transport 和 运行 请求:client.Do(req)
您在 http.Request
with (h Header) Set(key, value string)
上设置了页眉:
req.Header.Set("name", "value")
这是我发现的:
package main
import (
"fmt"
"io/ioutil"
"net/http"
)
var URL = "http://httpbin.org/ip"
func main() {
tr := &http.Transport{DisableKeepAlives: false}
req, _ := http.NewRequest("GET", URL, nil)
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", "Token"))
req.Close = false
res, err := tr.RoundTrip(req)
if err != nil {
fmt.Println(err)
}
body, _ := ioutil.ReadAll(res.Body)
fmt.Println(string(body))
}
而且有效。
对于您的特定问题,这可能不是您想要的 - 在请求中设置它对您的情况更有意义,但要直接回答您的问题,您应该能够添加默认值 header通过为您的传输使用自定义 RoundTrip
方法通过传输的所有请求。
查看 https://golang.org/pkg/net/http/#RoundTripper
类似于:
type CustomTransport struct {
http.RoundTripper
}
func (ct *CustomTransport) RoundTrip(req *http.Request) (*http.Response, error) {
req.Header.Add("header-key", "header-value")
return ct.RoundTripper.RoundTrip(req)
}
url := "http://localhost:8181/api/v1/resource"
tr := &CustomTransport{
DisableKeepAlives: false,
MaxIdleConns: 0,
MaxIdleConnsPerHost: 0,
IdleConnTimeout: time.Second * 10,
}
client := &http.Client{Transport: tr}
resp, err := client.Get(url)
当我无法直接访问 API 客户端库使用的 http
客户端(或每个请求 object 直接访问)时,我发现这很有用,但它允许我来传递一个交通工具。