英文:
Golang http request specify host in POST path
问题
我正在使用golang执行以下请求:
request := &http.Request{
URL: url,
Body: requestBody, //包含请求体的io.ReadCloser
Method: http.MethodPost,
ContentLength: int64(len(postBody)),
Header: make(http.Header),
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
}
res, err := http.DefaultClient.Do(request)
//打印请求:
dump, dumpErr := httputil.DumpRequest(request, true)
if dumpErr != nil {
log.Fatal("无法转储请求")
}
log.Println(string(dump))
我希望在POST请求的路径中指定主机。这可能吗?
期望的结果:
POST "http://127.0.0.1:10019/system?action=add_servers" HTTP/1.1
Host: 127.0.0.1:10019
Accept: "*/*"
Connection: keep-alive
实际结果:
POST "/system?action=add_servers" HTTP/1.1
Host: 127.0.0.1:10019
Accept: "*/*"
Connection: keep-alive
英文:
I'm performing the following request in golang:
request := &http.Request{
URL: url,
Body: requestBody, //io.ReadCloser containing the body
Method: http.MethodPost,
ContentLength: int64(len(postBody)),
Header: make(http.Header),
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
}
res, err := http.DefaultClient.Do(request)
//Printing the request:
dump, dumpErr := httputil.DumpRequest(request, true)
if dumpErr != nil {
log.Fatal("Cannot dump the request")
}
log.Println(string(dump))
I want the host to be specified also in the path of the post request. Is it possible?
Expected result:
POST "http://127.0.0.1:10019/system?action=add_servers" HTTP/1.1
Host: 127.0.0.1:10019
Accept: "*/*"
Connection: keep-alive
Actual result:
POST "/system?action=add_servers" HTTP/1.1
Host: 127.0.0.1:10019
Accept: "*/*"
Connection: keep-alive
答案1
得分: 2
将Request.URL设置为不透明URL。不透明URL将按原样写入请求行。
request := &http.Request{
URL: &url.URL{Opaque: "http://127.0.0.1:10019/system?action=add_servers"},
Body: requestBody, //包含请求体的io.ReadCloser
Method: http.MethodPost,
ContentLength: int64(len(postBody)),
Header: make(http.Header),
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
}
http.NewRequest和http.NewRequestContext函数是创建请求值的首选方法。在使用这些函数之一创建请求后,将Request.URL设置为不透明URL:
u := "http://127.0.0.1:10019/system?action=add_servers"
request, err := http.NewRequest("POST", u, requestBody)
if err != nil {
//处理错误
}
request.URL = &url.URL{Opaque: u}
res, err := http.DefaultClient.Do(request)
英文:
Set the Request.URL to an opaque URL. The opaque URL is written to the request line as is.
request := &http.Request{
URL: &url.URL{Opaque: "http://127.0.0.1:10019/system?action=add_servers"}
Body: requestBody, //io.ReadCloser containing the body
Method: http.MethodPost,
ContentLength: int64(len(postBody)),
Header: make(http.Header),
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
}
The http.NewRequest and http.NewRequestContext functions are the preferred way to create a request value. Set Request.URL to the opaque URL after creating the request with one of these functions:
u := "http://127.0.0.1:10019/system?action=add_servers"
request, err := http.NewRequest("POST", u, requestBody)
if err != nil {
// handle error
}
request.URL = &url.URL{Opaque: u}
res, err := http.DefaultClient.Do(request)
答案2
得分: 0
URL变量的值是什么?
我认为你可以使用特定的主机来定义URL变量
var url = "http://127.0.0.1:10019/system?action=add_servers"
如果你的路径是从另一个变量动态获取的,你可以使用fmt.Sprintf
,如下所示
// 假设url的值
var path = "/system?action=add_servers"
url = fmt.Sprintf("http://127.0.0.1:10019/%s", path)
英文:
what value of the URL variable?
I think you can define the URL variable use a specific host
var url = "http://127.0.0.1:10019/system?action=add_servers"
In case your path is dynamic from another variable, you can use fmt.Sprintf
, like below
// assume url value
var path = "/system?action=add_servers"
url = fmt.Sprintf("http://127.0.0.1:10019/%s", path)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论