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
+11.9k Golang : List objects in AWS S3 bucket
+4.4k Unix/Linux : How to archive and compress entire directory ?
+6.9k Golang : Gomobile init produce "iphoneos" cannot be located error
+2.6k Java : Get FX sentiment from website example
+5.3k Golang : Test input string for unicode example
+16.7k Golang : Convert IPv4 address to decimal number(base 10) or integer
+7.7k Golang : Find network service name from given port and protocol
+34.2k Golang : Validate IP address
+32.9k Golang : Integer is between a range
+6.3k Golang : Scanf function weird error in Windows
+11k Golang : Send data to /dev/null a.k.a blackhole with ioutil.Discard
+6.5k Golang : Detect sample rate, channels or latency with PortAudio