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
+4k Javascript : Empty an array example
+16.8k Golang : Capture stdout of a child process and act according to the result
+9.5k Golang : Copy map(hash table) example
+30.2k Golang : How to redirect to new page with net/http?
+9.9k CodeIgniter : Load different view for mobile devices
+27.2k Golang : Convert CSV data to JSON format and save to file
+20.6k Golang : Convert date string to variants of time.Time type examples
+5.4k Golang : Stop goroutine without channel
+11.4k Golang : Generate DSA private, public key and PEM files example
+10k Golang : How to tokenize source code with text/scanner package?
+8.6k Golang : Heap sort example
+6.7k How to let Facebook Login button redirect to a particular URL ?