From 15abc3066ad4bb0c7e016169d52efe4e19c0ddfd Mon Sep 17 00:00:00 2001 From: Aren Patel Date: Fri, 20 Jan 2017 18:06:44 -0800 Subject: [PATCH] Use constants for status codes, HTTP methods and JSON API media types in comments and readme --- README.md | 16 ++++++++-------- request.go | 2 +- response.go | 6 +++--- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index e155f92..8561bf4 100644 --- a/README.md +++ b/README.md @@ -229,17 +229,17 @@ func CreateBlog(w http.ResponseWriter, r *http.Request) { blog := new(Blog) if err := jsonapi.UnmarshalPayload(r.Body, blog); err != nil { - http.Error(w, err.Error(), 500) + http.Error(w, err.Error(), http.StatusInternalServerError) return } // ...save your blog... - w.WriteHeader(201) - w.Header().Set("Content-Type", "application/vnd.api+json") + w.WriteHeader(http.StatusCreated) + w.Header().Set("Content-Type", jsonapi.MediaType) if err := jsonapi.MarshalOnePayload(w, blog); err != nil { - http.Error(w, err.Error(), 500) + http.Error(w, err.Error(), http.StatusInternalServerError) } } ``` @@ -292,10 +292,10 @@ func ListBlogs(w http.ResponseWriter, r *http.Request) { // but, for now blogs := testBlogsForList() - w.WriteHeader(200) - w.Header().Set("Content-Type", "application/vnd.api+json") + w.WriteHeader(http.StatusOK) + w.Header().Set("Content-Type", jsonapi.MediaType) if err := jsonapi.MarshalManyPayload(w, blogs); err != nil { - http.Error(w, err.Error(), 500) + http.Error(w, err.Error(), http.StatusInternalServerError) } } ``` @@ -355,7 +355,7 @@ jsonapi.MarshalOnePayloadEmbedded(out, testModel()) h := new(BlogsHandler) w := httptest.NewRecorder() -r, _ := http.NewRequest("POST", "/blogs", out) +r, _ := http.NewRequest(http.MethodPost, "/blogs", out) h.CreateBlog(w, r) diff --git a/request.go b/request.go index 7d23739..1bb9d2c 100644 --- a/request.go +++ b/request.go @@ -54,7 +54,7 @@ var ( // // ...do stuff with your blog... // // w.WriteHeader(201) -// w.Header().Set("Content-Type", "application/vnd.api+json") +// w.Header().Set("Content-Type", jsonapi.MediaType) // // if err := jsonapi.MarshalOnePayload(w, blog); err != nil { // http.Error(w, err.Error(), 500) diff --git a/response.go b/response.go index 70837a7..74d5ca1 100644 --- a/response.go +++ b/response.go @@ -97,10 +97,10 @@ func MarshalOne(model interface{}) (*OnePayload, error) { // // blogs := testBlogsForList() // -// w.WriteHeader(200) -// w.Header().Set("Content-Type", "application/vnd.api+json") +// w.WriteHeader(http.StatusOK) +// w.Header().Set("Content-Type", jsonapi.MediaType) // if err := jsonapi.MarshalManyPayload(w, blogs); err != nil { -// http.Error(w, err.Error(), 500) +// http.Error(w, err.Error(), http.StatusInternalServerError) // } // } //