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
+4.1k Golang : How to solve "too many .rsrc sections" error?
+3.9k Javascript : How to loop over and parse JSON data?
+4.6k Javascript : Generate random key with specific length
+10.2k Golang : calculate elapsed run time
+4.9k Golang : Calculate diameter, circumference, area, sphere surface and volume
+5.8k Golang : Check to see if *File is a file or directory
+10.8k Golang : Handle or parse date string with Z suffix(RFC3339) example
+25.9k Golang : Save map/struct to JSON or XML file
+16.6k Golang : Get path name to current directory or folder
+11.7k Golang : How to get URL port?
+7.4k Golang : Get all countries currencies code in JSON format
+5k Unix/Linux : How to fix CentOS yum duplicate glibc or device-mapper-libs dependency error?