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
+31.1k Golang : Get local IP and MAC address
+12.3k Golang : Get absolute path to binary for os.Exec function with exec.LookPath
+14k Golang : Convert IP version 6 address to integer or decimal number
+4.6k Golang : A program that contain another program and executes it during run-time
+7.9k Golang : Tell color name with OpenCV example
+13.1k Golang : error parsing regexp: invalid or unsupported Perl syntax
+4.4k JavaScript : Rounding number to decimal formats to display currency
+9.4k Random number generation with crypto/rand in Go
+7.7k Golang : Handle Palindrome string with case sensitivity and unicode
+5.8k PageSpeed : Clear or flush cache on web server
+19.9k Golang : How to get struct tag and use field name to retrieve data?