diff options
Diffstat (limited to 'app/internal/utils/json.go')
| -rw-r--r-- | app/internal/utils/json.go | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/app/internal/utils/json.go b/app/internal/utils/json.go new file mode 100644 index 0000000..d3c6b71 --- /dev/null +++ b/app/internal/utils/json.go @@ -0,0 +1,22 @@ +package utils + +import ( + "encoding/json" + "net/http" +) + +func RespondWithError(w http.ResponseWriter, code int, message string) { + RespondWithJSON(w, map[string]string{"error": message}, code) +} + +func RespondWithJSON(w http.ResponseWriter, payload interface{}, code ...int) { + var c int = http.StatusOK + if len(code) > 0 { + c = code[0] + } + response, _ := json.Marshal(payload) + + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(c) + w.Write(response) +} |
