Skip to content

Instantly share code, notes, and snippets.

@olafkotur
Created January 29, 2020 20:34
Show Gist options
  • Save olafkotur/881862ac3a2c4a50d5cb9b6d5d60fc86 to your computer and use it in GitHub Desktop.
Save olafkotur/881862ac3a2c4a50d5cb9b6d5d60fc86 to your computer and use it in GitHub Desktop.
HTTP requests in Golang
// GET: Send the request
res, err := http.Get("http://example.com/api/someContext")
if err != nil {
log.Println(err)
}
// Read HTTP response into []byte
body, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Println(err)
}
res.Body.Close()
// Map into into a meaningful type
err = json.Unmarshal(body, &someType)
// POST: Define the form values
values := url.Values{
"someValue": thatValue,
"someOtherValue": thatOtherValue,
}
// Send the request
res, err := http.PostForm("http://example.com/api/someContext", values)
if err != nil {
log.Println(err)
}
res.Body.Close() // Assuming we ignore the result, otherwise see HTTP GET
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment