From 38ec6e3493051b7ab4dd84b30a3fae22256e273e Mon Sep 17 00:00:00 2001 From: Martijn Broenland Date: Thu, 1 Sep 2022 18:05:14 +0200 Subject: [PATCH 1/4] Added RespondJSON() --- respond/json.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100755 respond/json.go diff --git a/respond/json.go b/respond/json.go new file mode 100755 index 0000000..070a366 --- /dev/null +++ b/respond/json.go @@ -0,0 +1,16 @@ +package respond + +import ( + "encoding/json" + + "github.com/Jille/convreq/internal" +) + +// RespondJSON creates a JSON response +func RespondJSON(data interface{}) internal.HttpResponse { + response, err := json.Marshal(data) + if err != nil { + return InternalServerError(err.Error()) + } + return WithHeader(Bytes(response), "Content-Type", "application/json") +} From 60e1f4bd54d7dec8f2b2daff7245cee4060cc44b Mon Sep 17 00:00:00 2001 From: Martijn Broenland Date: Mon, 5 Sep 2022 17:22:52 +0200 Subject: [PATCH 2/4] Improved implementation for JSON support --- respond/json.go | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/respond/json.go b/respond/json.go index 070a366..70794e7 100755 --- a/respond/json.go +++ b/respond/json.go @@ -2,15 +2,22 @@ package respond import ( "encoding/json" + "net/http" "github.com/Jille/convreq/internal" ) -// RespondJSON creates a JSON response -func RespondJSON(data interface{}) internal.HttpResponse { - response, err := json.Marshal(data) - if err != nil { - return InternalServerError(err.Error()) - } - return WithHeader(Bytes(response), "Content-Type", "application/json") +type respondJSON struct { + data interface{} +} + +// Respond implements convreq.HttpResponse. +func (rj respondJSON) Respond(w http.ResponseWriter, r *http.Request) error { + w.Header().Set("Content-Type", "application/json") + return json.NewEncoder(w).Encode(rj.data) +} + +// ServeJSON uses json.Marshal() to serve a JSON object. +func ServeJSON(data interface{}) internal.HttpResponse { + return respondJSON{data} } From d0be3f77e3c317e141f201f56bef4c520a7f2d85 Mon Sep 17 00:00:00 2001 From: Martijn Broenland Date: Tue, 6 Sep 2022 09:40:56 +0200 Subject: [PATCH 3/4] Only set content-type header for JSON request if not already set --- respond/json.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/respond/json.go b/respond/json.go index 70794e7..98dd09d 100755 --- a/respond/json.go +++ b/respond/json.go @@ -13,7 +13,9 @@ type respondJSON struct { // Respond implements convreq.HttpResponse. func (rj respondJSON) Respond(w http.ResponseWriter, r *http.Request) error { - w.Header().Set("Content-Type", "application/json") + if w.Header.Get("Content-Type") == "" { + w.Header().Set("Content-Type", "application/json") + } return json.NewEncoder(w).Encode(rj.data) } From 4ccb624787adc855857c7b0f5e8a3b3fc6084530 Mon Sep 17 00:00:00 2001 From: Martijn Broenland Date: Tue, 6 Sep 2022 09:42:03 +0200 Subject: [PATCH 4/4] Typofix --- respond/json.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/respond/json.go b/respond/json.go index 98dd09d..39ef24b 100755 --- a/respond/json.go +++ b/respond/json.go @@ -13,7 +13,7 @@ type respondJSON struct { // Respond implements convreq.HttpResponse. func (rj respondJSON) Respond(w http.ResponseWriter, r *http.Request) error { - if w.Header.Get("Content-Type") == "" { + if w.Header().Get("Content-Type") == "" { w.Header().Set("Content-Type", "application/json") } return json.NewEncoder(w).Encode(rj.data)