Skip to content

Instantly share code, notes, and snippets.

@karl-gustav
Last active August 22, 2022 08:00
Show Gist options
  • Save karl-gustav/e5cf2b779ba3127c204d08d0af93d540 to your computer and use it in GitHub Desktop.
Save karl-gustav/e5cf2b779ba3127c204d08d0af93d540 to your computer and use it in GitHub Desktop.
Encode/decode directly into golang http
var subscription Subscription
err := json.NewDecoder(r.Body).Decode(&subscription)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
defer r.Body.Close()
w.Header().Set("Content-Type", "application/json")
subscription, _ := getSubscription()
err = json.NewEncoder(w).Encode(subscription)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment