From 5d477aac189b4004b562a15dff2c13c6d2e958c2 Mon Sep 17 00:00:00 2001 From: MikeIsAStar <99037623+MikeIsAStar@users.noreply.github.com> Date: Sun, 7 Jan 2024 19:00:00 -0500 Subject: [PATCH] API: Do not return 'null' when no groups exist --- api/groups.go | 16 ++++++++++------ api/stats.go | 3 +-- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/api/groups.go b/api/groups.go index 62cd1dc..54e3b77 100644 --- a/api/groups.go +++ b/api/groups.go @@ -10,8 +10,6 @@ import ( ) func HandleGroups(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "application/json") - u, err := url.Parse(r.URL.String()) if err != nil { w.WriteHeader(http.StatusBadRequest) @@ -47,12 +45,18 @@ func HandleGroups(w http.ResponseWriter, r *http.Request) { } } - jsonData, err := json.Marshal(groups) - if err != nil { - w.WriteHeader(http.StatusInternalServerError) - return + var jsonData []byte + if len(groups) == 0 { + jsonData, _ = json.Marshal([]string{}) + } else { + jsonData, err = json.Marshal(groups) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + return + } } + w.Header().Set("Content-Type", "application/json") w.Header().Set("Access-Control-Allow-Origin", "*") w.Header().Set("Content-Length", strconv.Itoa(len(jsonData))) w.Write(jsonData) diff --git a/api/stats.go b/api/stats.go index 47d9208..522e0a9 100644 --- a/api/stats.go +++ b/api/stats.go @@ -16,8 +16,6 @@ type Stats struct { } func HandleStats(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "application/json") - u, err := url.Parse(r.URL.String()) if err != nil { w.WriteHeader(http.StatusBadRequest) @@ -85,6 +83,7 @@ func HandleStats(w http.ResponseWriter, r *http.Request) { return } + w.Header().Set("Content-Type", "application/json") w.Header().Set("Access-Control-Allow-Origin", "*") w.Header().Set("Content-Length", strconv.Itoa(len(jsonData))) w.Write(jsonData)