2017-10-18 162 views
-5

我試圖做這樣的請求:做與查詢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

回答

2

它簡單地把它添加到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)