Skip to content

Instantly share code, notes, and snippets.

@eyeezzi
Created February 21, 2018 18:44
Show Gist options
  • Save eyeezzi/7d9bb5880a4fc18d9ea4915517a256e9 to your computer and use it in GitHub Desktop.
Save eyeezzi/7d9bb5880a4fc18d9ea4915517a256e9 to your computer and use it in GitHub Desktop.
func uploadHandler(w http.ResponseWriter, r *http.Request) {
var m map[string]interface{}
body, err := ioutil.ReadAll(r.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// fmt.Println(string(body))
// json.Unmarshal(b, &t) => decode the byte array into a type
if err = json.Unmarshal(body, &m); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// json.Marshal(m) => encode the object m as a byte array containing its json representation.
jsonStr, err := json.Marshal(m)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
fmt.Fprintln(w, string(jsonStr))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment