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
+9.8k Golang : Web routing/multiplex example
+12.1k Golang : Generate Code128 barcode
+23.5k Golang : convert rune to integer value
+32.4k Golang : How to check if slice or array is empty?
+9.4k Golang : ISO8601 Duration Parser example
+9.4k Golang : Underscore string example
+8.9k Golang : Channels and buffered channels examples
+13.5k Golang : Execute function at intervals or after some delay
+20.4k Golang : Convert seconds to minutes and remainder seconds
+5.1k AWS S3 : Prevent Hotlinking policy
+7.6k Golang : Executing and evaluating nested loop in html template