From 81207681fc7ed2ba1c2f2bb9ccc27005e1ffa80c Mon Sep 17 00:00:00 2001 From: Unbewohnte Date: Thu, 7 Oct 2021 08:58:51 +0300 Subject: [PATCH] Set content-type to GET requests, fixed ToJsonAll returning invalid json --- dbHandle/handle.go | 4 +++- randomData/randomData.go | 16 ++++------------ 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/dbHandle/handle.go b/dbHandle/handle.go index 21930af..e86efe0 100644 --- a/dbHandle/handle.go +++ b/dbHandle/handle.go @@ -155,13 +155,14 @@ func (db *DB) HandleGlobalWeb(w http.ResponseWriter, r *http.Request) { log.Printf("Could not retrieve db contents: %s\n", err) return } - randomDatasJsonBytes, err := randomdata.ToJsonAll(randomDatas, true) + randomDatasJsonBytes, err := randomdata.ToJsonAll(randomDatas) if err != nil { w.WriteHeader(http.StatusInternalServerError) log.Printf("Could not convert to json: %s\n", err) return } + w.Header().Add("content-type", "application/json") w.Write(randomDatasJsonBytes) default: @@ -197,6 +198,7 @@ func (db *DB) HandleSpecificWeb(w http.ResponseWriter, r *http.Request) { return } + w.Header().Add("content-type", "application/json") w.Write(rdJsonBytes) case http.MethodDelete: diff --git a/randomData/randomData.go b/randomData/randomData.go index 4979e47..436daa6 100644 --- a/randomData/randomData.go +++ b/randomData/randomData.go @@ -35,18 +35,10 @@ func (rd *RandomData) ToJson() ([]byte, error) { } // Convert all rdatas to []byte -func ToJsonAll(rdatas []*RandomData, addNewlines bool) ([]byte, error) { - var rdatasBytes []byte - for _, rdata := range rdatas { - jsonBytes, err := rdata.ToJson() - if err != nil { - return nil, err - } - rdatasBytes = append(rdatasBytes, jsonBytes...) - - if addNewlines { - rdatasBytes = append(rdatasBytes, 10) - } +func ToJsonAll(rdatas []*RandomData) ([]byte, error) { + rdatasBytes, err := json.Marshal(&rdatas) + if err != nil { + return nil, err } return rdatasBytes, nil