Skip to content

Commit

Permalink
Update documentation for responding with proper JSON API headers (goo…
Browse files Browse the repository at this point in the history
…gle#84)

* Update documentation for responding with proper JSONAPI headers

* Fixed examples and commented code for sending proper response headers
  • Loading branch information
angelospanag authored and aren55555 committed Apr 5, 2017
1 parent bce7629 commit af3dab1
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 6 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down
3 changes: 2 additions & 1 deletion examples/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion response.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
// }
Expand Down

0 comments on commit af3dab1

Please sign in to comment.