Skip to content

Instantly share code, notes, and snippets.

@cdemers
Created May 5, 2016 13:33
Show Gist options
  • Save cdemers/76569d3c2b72f0f2fecb086d8a01ca0e to your computer and use it in GitHub Desktop.
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.
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