问题描述
我发现go http包非常混乱。我认为客户端和服务器应该分成两个不同的包,也许是第三个包含常见的东西。
I find the go http package quite confusing. I think the client and server should be separated in two different package and perhaps a 3rd with the common stuff.
无论如何尝试创建POST请求,但我无法获得它完成了。没有收到另一方。
这是应该如何工作的?我知道功能,但我认为我不能使用它,因为它可以'您可以使用进行测试吗?
Anyway trying to make a POST request but I can't get it done. Nothing is received on the other side. Is this how it is supposed to work ? I'm aware of the PostForm function but I think I can't use it because it can't be tested with httputil right ?
hc := http.Client{}
req, err := http.NewRequest("POST", APIURL, nil)
form := url.Values{}
form.Add("ln", c.ln)
form.Add("ip", c.ip)
form.Add("ua", c.ua)
req.PostForm = form
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
glog.Info("form was %v", form)
resp, err := hc.Do(req)
推荐答案
你大多是正确的想法,它只是发送错误的表单。
You have mostly the right idea, it's just the sending of the form that is wrong. The form belongs in the body of the request.
req, err := http.NewRequest("POST", url, strings.NewReader(form.Encode()))
这篇关于如何在Golang中发送POST请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!