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
+27.2k Golang : Call a function after some delay(time.Sleep and Tick)
+10.4k Golang : How to remove certain lines from a file
+12k Golang : convert string or integer to big.Int type
+9.1k Golang : Increment string example
+7k Golang : Function wrapper that takes arguments and return result example
+3k Unix/Linux : How to pipe/save output of a command to file?
+14.4k Golang : Logging with logrus
+8.1k Golang : Simple File Server
+4.6k Fix sudo yum hang problem with no output or error messages
+6.4k Golang : Quadratic example
+18.9k Golang : Read directory content with filepath.Walk()
+9.3k Golang : Fix image: unknown format error