API: Do not return 'null' when no groups exist

This commit is contained in:
MikeIsAStar 2024-01-07 19:00:00 -05:00
parent d9945b29f5
commit 5d477aac18
2 changed files with 11 additions and 8 deletions

View File

@ -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)

View File

@ -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)