Golang : Post data with url.Values{}
Just a short example on how to use url.Values{}. This code fragment is taken from previous tutorial on how to store cookies into cookie Jar.
Here you go :
urlData := url.Values{}
urlData.Set("search_query", "macross")
req, _ := http.NewRequest("POST", "https://www.youtube.com/results?search_query=", strings.NewReader(urlData.Encode()))
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp, err := client.Do(req)
if err != nil {
panic(nil)
}
body, _ := ioutil.ReadAll(resp.Body)
resp.Body.Close()
// display content to screen ... save this to a HTML file and view the file with browser ;-)
fmt.Println(string(body))
Happy coding!
See also : Golang : Storing cookies in http.CookieJar example
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+13.6k Golang : Read XML elements data with xml.CharData example
+5.8k Golang : ROT32768 (rotate by 0x80) UTF-8 strings example
+11.6k Get form post value in Go
+6.1k Golang : Function as an argument type example
+6.3k Golang : Calculate US Dollar Index (DXY)
+5.8k Unix/Linux/MacOSx : Get local IP address
+5.7k Golang : Shortening import identifier
+8.2k Android Studio : Rating bar example
+5.5k Gogland : Datasource explorer
+11.2k Golang : Simple image viewer with Go-GTK
+15.3k Golang : Accurate and reliable decimal calculations
+22.8k Golang : Round float to precision example