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
+21.6k Golang : Calculate time different
+24.6k Mac/Linux and Golang : Fix bind: address already in use error
+11.2k Golang : Extract part of string with regular expression
+12.6k Golang : Gin framework accept query string by post request example
+9k Android Studio : Simple input textbox and intercept key example
+11.6k Golang : Transform comma separated string to slice example
+10.4k Golang : Secure file deletion with wipe example
+15k Golang : Execute terminal command to remote machine example
+4.2k Python : Create Whois client or function example
+7k Golang : Append and add item in slice
+14.7k Golang : Read a file line by line