Skip to content

Instantly share code, notes, and snippets.

@bussiere
Created November 28, 2018 10:10
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/56413e46bd32406b48669c89d201d38d to your computer and use it in GitHub Desktop.
Save bussiere/56413e46bd32406b48669c89d201d38d to your computer and use it in GitHub Desktop.
Receive a json in golang
func test(w http.ResponseWriter, r *http.Request) {
var test = Data{}
var reception = Data{}
test.Fruit = "Banana"
body, err := ioutil.ReadAll(r.Body)
if err != nil {
panic(err)
}
err = json.Unmarshal(body, &reception)
fmt.Println(reception.Fruit)
js, err := json.Marshal(test)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(js)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment