From b3cadcb01ff6e39c1bb7e940d55f9ff68401b381 Mon Sep 17 00:00:00 2001 From: Jordan Wright Date: Wed, 18 Jan 2017 20:12:25 -0600 Subject: [PATCH] Fixing middleware JSON responses --- middleware/middleware.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/middleware/middleware.go b/middleware/middleware.go index 6b429817..5756caa1 100644 --- a/middleware/middleware.go +++ b/middleware/middleware.go @@ -73,6 +73,7 @@ func RequireAPIKey(handler http.Handler) http.HandlerFunc { } if ak == "" { JSONError(w, 400, "API Key not set") + return } else { u, err := models.GetUserByAPIKey(ak) if err != nil { @@ -101,8 +102,8 @@ func RequireLogin(handler http.Handler) http.HandlerFunc { // JSONError returns an error in JSON format with the given // status code and message func JSONError(w http.ResponseWriter, c int, m string) { - w.WriteHeader(c) - w.Header().Set("Content-Type", "application/json") cj, _ := json.MarshalIndent(models.Response{Success: false, Message: m}, "", " ") + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(c) fmt.Fprintf(w, "%s", cj) }