Created
May 5, 2016 13:33
-
-
Save cdemers/76569d3c2b72f0f2fecb086d8a01ca0e to your computer and use it in GitHub Desktop.
Sample Go program that does an HTTP post of a JSON payload, includes setting request headers, doing the request, getting the response and printing it out.
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
func main() { | |
var jsonBuffer = []byte(`{"my_key":"My Value."}`) | |
req, _ := http.NewRequest("POST", "http://api.mycompany.com", bytes.NewBuffer(jsonBuffer)) | |
req.Header.Set("Content-Type", "application/json") | |
client := &http.Client{} | |
resp, err := client.Do(req) | |
if err != nil { | |
panic(err) | |
} | |
defer resp.Body.Close() | |
body, _ := ioutil.ReadAll(resp.Body) | |
fmt.Println("HTTP Response Status:", resp.Status) | |
fmt.Println("HTTP response Headers:", resp.Header) | |
fmt.Println("HTTP response Body:", string(body)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment