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
+8.1k Golang: Prevent over writing file with md5 hash
+6.9k Golang : Squaring elements in array
+9.5k Golang : Qt get screen resolution and display on center example
+12.8k Golang : Convert(cast) uintptr to string example
+22.2k Golang : Convert Unix timestamp to UTC timestamp
+17.3k Golang : Defer function inside init()
+6.6k Mac OSX : Find large files by size
+19.8k Swift : Convert (cast) Int to int32 or Uint32
+10.2k Golang : Generate random integer or float number
+8k Golang : Reverse text lines or flip line order example
+7k Ubuntu : connect() to unix:/var/run/php5-fpm.sock failed (13: Permission denied) while connecting to upstream
+8.1k Golang : Convert word to its plural form example