Skip to content

Instantly share code, notes, and snippets.

@JakeTheCorn
Created July 13, 2018 17:39
Show Gist options
  • Save JakeTheCorn/1002e6b59d04bd554ac2ab689369c22c to your computer and use it in GitHub Desktop.
Save JakeTheCorn/1002e6b59d04bd554ac2ab689369c22c to your computer and use it in GitHub Desktop.
Post Json in Golang
func createFriend() {
requestURL := "http://rest.learncode.academy/api/johnbob/friends"
payload := strings.NewReader("{\"FirstName\": \"Billy\"}")
req, _ := http.NewRequest("POST", requestURL, payload)
req.Header.Add("content-type", "application/json")
res, err := http.DefaultClient.Do(req)
if err != nil {
panic(err)
}
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)
fmt.Println(res)
fmt.Println(string(body))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment