Last active
August 22, 2022 08:00
-
-
Save karl-gustav/e5cf2b779ba3127c204d08d0af93d540 to your computer and use it in GitHub Desktop.
Encode/decode directly into golang http
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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