From e083f806866814308291f4f47942eb97fb317740 Mon Sep 17 00:00:00 2001 From: Angelos Panagiotopoulos Date: Wed, 5 Apr 2017 12:04:56 +0100 Subject: [PATCH 1/2] Update documentation for responding with proper JSONAPI headers --- README.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index dc59105..99e817b 100644 --- a/README.md +++ b/README.md @@ -232,8 +232,8 @@ func CreateBlog(w http.ResponseWriter, r *http.Request) { // ...save your blog... - w.WriteHeader(http.StatusCreated) w.Header().Set("Content-Type", jsonapi.MediaType) + w.WriteHeader(http.StatusCreated) if err := jsonapi.MarshalOnePayload(w, blog); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) @@ -288,8 +288,9 @@ func ListBlogs(w http.ResponseWriter, r *http.Request) { // but, for now blogs := testBlogsForList() - w.WriteHeader(http.StatusOK) w.Header().Set("Content-Type", jsonapi.MediaType) + w.WriteHeader(http.StatusOK) + if err := jsonapi.MarshalManyPayload(w, blogs); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } @@ -325,8 +326,9 @@ func CreateBlogs(w http.ResponseWriter, r *http.Request) { // ...save each of your blogs } - w.WriteHeader(http.StatusCreated) w.Header().Set("Content-Type", jsonapi.MediaType) + w.WriteHeader(http.StatusCreated) + if err := jsonapi.MarshalManyPayload(w, blogs); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } From 1c60ae95d1975d6092e4cf3e90a958b028689fc6 Mon Sep 17 00:00:00 2001 From: Angelos Panagiotopoulos Date: Wed, 5 Apr 2017 13:21:20 +0100 Subject: [PATCH 2/2] Fixed examples and commented code for sending proper response headers --- examples/handler.go | 3 ++- request.go | 2 +- response.go | 3 ++- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/examples/handler.go b/examples/handler.go index eaf41d0..bba5c24 100644 --- a/examples/handler.go +++ b/examples/handler.go @@ -105,8 +105,9 @@ func (h *ExampleHandler) listBlogs(w http.ResponseWriter, r *http.Request) { // but, for now blogs := fixtureBlogsList() - w.WriteHeader(http.StatusOK) w.Header().Set("Content-Type", jsonapi.MediaType) + w.WriteHeader(http.StatusOK) + if err := jsonapiRuntime.MarshalManyPayload(w, blogs); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } diff --git a/request.go b/request.go index 335ecb4..ea6ae45 100644 --- a/request.go +++ b/request.go @@ -55,8 +55,8 @@ var ( // // // ...do stuff with your blog... // -// w.WriteHeader(201) // w.Header().Set("Content-Type", jsonapi.MediaType) +// w.WriteHeader(201) // // if err := jsonapi.MarshalOnePayload(w, blog); err != nil { // http.Error(w, err.Error(), 500) diff --git a/response.go b/response.go index fcee64b..a8ac71f 100644 --- a/response.go +++ b/response.go @@ -122,8 +122,9 @@ func MarshalManyPayloadWithoutIncluded(w io.Writer, models interface{}) error { // // blogs := testBlogsForList() // -// w.WriteHeader(http.StatusOK) // w.Header().Set("Content-Type", jsonapi.MediaType) +// w.WriteHeader(http.StatusOK) +// // if err := jsonapi.MarshalManyPayload(w, blogs); err != nil { // http.Error(w, err.Error(), http.StatusInternalServerError) // }