当我尝试从简单proxy
重新发送请求时
http.HandleFunc("/",func(w http.ResponseWriter, r *http.Request) {
log.Printf("proxy rq: %v", r)
client := &http.Client{}
proxyRes, err := client.Do(r) // 👈🏻 Get "http://localhost:8097/tmp": http: Request.RequestURI can't be set in client requests
if err != nil {
log.Fatalf("err proxy request: %v",err)
}
resBody := proxyRes.Body
defer resBody.Close()
if _, err := io.Copy(w, resBody); err != nil {
log.Printf("copy error:%v\n", err)
}
})
http.ListenAndServe(":8099", nil)
,设置http_proxy
ENV(通过我的代理发送请求)
% export http_proxy=http://localhost:8099
% curl -v http://localhost:8097/tmp
我得到一个error
,就像
Get "http://localhost:8097/tmp": http: Request.RequestURI can't be set in client requests
我错过了什么?
2条答案
按热度按时间bhmjp9jg1#
不能将客户端请求用作
Do
的参数。请使用与r
相同的参数创建新请求,然后对此请求执行Do
qyzbxkaa2#
../src/net/http/client.go:217
(https://go.dev/src/net/http/client.go)中的误差定义为:如果在发送HTTP请求之前设置
r.RequestURI = ""
,则应该能够重用现有的r *http.Request
: