From 14a4ebeaaa5fb44d22bcde042b1f489185852a06 Mon Sep 17 00:00:00 2001 From: Patrick Seeburger Date: Tue, 5 Feb 2019 17:40:59 +0100 Subject: [PATCH] Using json.Encoder to write the output JSON instead of using a buffer. --- endpoints/endpoint.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/endpoints/endpoint.go b/endpoints/endpoint.go index 709e995..8138f83 100644 --- a/endpoints/endpoint.go +++ b/endpoints/endpoint.go @@ -73,8 +73,6 @@ func Endpoint(wrapped endpoint) httprouter.Handle { res[k] = v } - js, _ := json.Marshal(res) - w.Header().Set("Content-Type", "application/json") // Check if compression is supported @@ -83,9 +81,11 @@ func Endpoint(wrapped endpoint) httprouter.Handle { w.Header().Set("Content-Encoding", "gzip") gz := gzip.NewWriter(w) defer gz.Close() - gz.Write(js) + json := json.NewEncoder(gz) + json.Encode(res) } else { - w.Write(js) // Fall back to uncompressed response + json := json.NewEncoder(w) + json.Encode(res) // Fall back to uncompressed response } } }