做与查询GET请求参数

做与查询GET请求参数

问题描述:

我试图做这样的请求:做与查询GET请求参数

http://example.com/hello?name=username 

但是在文档,我不能找到一种方法,通过有效载荷参数。 (http.Get()只接收url)

我该怎么做这个请求?

+2

这是网址。 'http.Get(“http://example.com/hello?name=username”)' – captncraig

+0

是的,但有没有办法做到这一点传递有效载荷像在Python请求库? – Vivi

+2

如果需要,您可以使用['url.Values'](https://golang.org/pkg/net/url/#Values)对参数进行编码。 – JimB

它简单地把它添加到URL的最简单方法:

http.Get("http://example.com/hello?name=username") 

我喜欢做的是使用url.Values建立查询字符串的方式:

v := url.Values{} 
v.Set("name", "username") 
url := fmt.Sprintf("http://example.com/hello?%s", v.Encode()) 
http.Get(url)