From 04b3defc449973ec06f731b443acebe64cc64362 Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Sun, 10 Nov 2019 11:19:03 -0500 Subject: Fix responses sent to client JSON is no longer output to stdout and actually sent to client --- backend/main.go | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/backend/main.go b/backend/main.go index 3dd9d38..1ed188a 100644 --- a/backend/main.go +++ b/backend/main.go @@ -51,11 +51,10 @@ func RecipeList(w http.ResponseWriter, r *http.Request) { } resp.Data = append(resp.Data, APIDataIds{Ids: ids}) - output, err := json.MarshalIndent(resp, "", " ") - if err != nil { - fmt.Println("Error converting to JSON") - } else { - fmt.Println(string(output)) + w.Header().Set("Content-Type", "application/json; charset=UTF-8") + w.WriteHeader(http.StatusOK) + if err := json.NewEncoder(w).Encode(resp); err != nil { + panic(err) } } } @@ -78,11 +77,10 @@ func SingleRecipe(w http.ResponseWriter, r *http.Request) { } resp.Data = append(resp.Data, APIDataRecipe{recipe}) - output, err := json.MarshalIndent(resp, "", " ") - if err != nil { - fmt.Println("Error converting to JSON") - } else { - fmt.Println(string(output)) + w.Header().Set("Content-Type", "application/json; charset=UTF-8") + w.WriteHeader(http.StatusOK) + if err := json.NewEncoder(w).Encode(resp); err != nil { + panic(err) } } else if r.Method == "POST" { -- cgit v1.1