Skip to content

Commit af3dab1

Browse files
angelospanagaren55555
authored andcommitted
Update documentation for responding with proper JSON API headers (#84)
* Update documentation for responding with proper JSONAPI headers * Fixed examples and commented code for sending proper response headers
1 parent bce7629 commit af3dab1

File tree

4 files changed

+10
-6
lines changed

4 files changed

+10
-6
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,8 @@ func CreateBlog(w http.ResponseWriter, r *http.Request) {
232232

233233
// ...save your blog...
234234

235-
w.WriteHeader(http.StatusCreated)
236235
w.Header().Set("Content-Type", jsonapi.MediaType)
236+
w.WriteHeader(http.StatusCreated)
237237

238238
if err := jsonapi.MarshalOnePayload(w, blog); err != nil {
239239
http.Error(w, err.Error(), http.StatusInternalServerError)
@@ -288,8 +288,9 @@ func ListBlogs(w http.ResponseWriter, r *http.Request) {
288288
// but, for now
289289
blogs := testBlogsForList()
290290

291-
w.WriteHeader(http.StatusOK)
292291
w.Header().Set("Content-Type", jsonapi.MediaType)
292+
w.WriteHeader(http.StatusOK)
293+
293294
if err := jsonapi.MarshalManyPayload(w, blogs); err != nil {
294295
http.Error(w, err.Error(), http.StatusInternalServerError)
295296
}
@@ -325,8 +326,9 @@ func CreateBlogs(w http.ResponseWriter, r *http.Request) {
325326
// ...save each of your blogs
326327
}
327328

328-
w.WriteHeader(http.StatusCreated)
329329
w.Header().Set("Content-Type", jsonapi.MediaType)
330+
w.WriteHeader(http.StatusCreated)
331+
330332
if err := jsonapi.MarshalManyPayload(w, blogs); err != nil {
331333
http.Error(w, err.Error(), http.StatusInternalServerError)
332334
}

examples/handler.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,9 @@ func (h *ExampleHandler) listBlogs(w http.ResponseWriter, r *http.Request) {
105105
// but, for now
106106
blogs := fixtureBlogsList()
107107

108-
w.WriteHeader(http.StatusOK)
109108
w.Header().Set("Content-Type", jsonapi.MediaType)
109+
w.WriteHeader(http.StatusOK)
110+
110111
if err := jsonapiRuntime.MarshalManyPayload(w, blogs); err != nil {
111112
http.Error(w, err.Error(), http.StatusInternalServerError)
112113
}

request.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ var (
5555
//
5656
// // ...do stuff with your blog...
5757
//
58-
// w.WriteHeader(201)
5958
// w.Header().Set("Content-Type", jsonapi.MediaType)
59+
// w.WriteHeader(201)
6060
//
6161
// if err := jsonapi.MarshalOnePayload(w, blog); err != nil {
6262
// http.Error(w, err.Error(), 500)

response.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,9 @@ func MarshalManyPayloadWithoutIncluded(w io.Writer, models interface{}) error {
122122
//
123123
// blogs := testBlogsForList()
124124
//
125-
// w.WriteHeader(http.StatusOK)
126125
// w.Header().Set("Content-Type", jsonapi.MediaType)
126+
// w.WriteHeader(http.StatusOK)
127+
//
127128
// if err := jsonapi.MarshalManyPayload(w, blogs); err != nil {
128129
// http.Error(w, err.Error(), http.StatusInternalServerError)
129130
// }

0 commit comments

Comments
 (0)