Created
January 29, 2020 20:34
-
-
Save olafkotur/881862ac3a2c4a50d5cb9b6d5d60fc86 to your computer and use it in GitHub Desktop.
HTTP requests in Golang
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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