Skip to content

Instantly share code, notes, and snippets.

@bussiere
Created November 28, 2018 10:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bussiere/e38f45ef74c15a8e6ba15315ba6d48cb to your computer and use it in GitHub Desktop.
Save bussiere/e38f45ef74c15a8e6ba15315ba6d48cb to your computer and use it in GitHub Desktop.
Send json with go
func main() {
var reception = Data{}
var sendData = Data{}
sendData.Fruit = "Melon"
url := "http://localhost:8309/"
fmt.Println("URL:>", url)
js, err := json.Marshal(sendData)
req, err := http.NewRequest("GET", url, bytes.NewBuffer(js))
req.Header.Set("X-Custom-Header", "myvalue")
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
fmt.Println("response Status:", resp.Status)
fmt.Println("response Headers:", resp.Header)
body, _ := ioutil.ReadAll(resp.Body)
fmt.Println("response Body:", string(body))
err = json.Unmarshal(body, &reception)
fmt.Println(reception.Fruit)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment