Permalink
Browse files

fix(example/shipping): header should set before `WriteHeader`

From `net/http`:

    Changing the header map after a call to WriteHeader (or Write)
    has no effect unless the modified headers are trailers.

To send the `content-type`, need to set before calling `WriteHeader`.

Fixes #537
  • Loading branch information...
bcho committed May 26, 2017
1 parent 0012c63 commit 8392035d8b370244e5be7a16a6c13726db8be943
@@ -179,6 +179,7 @@ type errorer interface {
// encode errors from business-logic
func encodeError(_ context.Context, err error, w http.ResponseWriter) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
switch err {
case cargo.ErrUnknown:
w.WriteHeader(http.StatusNotFound)
@@ -187,7 +188,6 @@ func encodeError(_ context.Context, err error, w http.ResponseWriter) {
default:
w.WriteHeader(http.StatusInternalServerError)
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
json.NewEncoder(w).Encode(map[string]interface{}{
"error": err.Error(),
})
@@ -85,6 +85,7 @@ type errorer interface {
// encode errors from business-logic
func encodeError(_ context.Context, err error, w http.ResponseWriter) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
switch err {
case cargo.ErrUnknown:
w.WriteHeader(http.StatusNotFound)
@@ -93,7 +94,6 @@ func encodeError(_ context.Context, err error, w http.ResponseWriter) {
default:
w.WriteHeader(http.StatusInternalServerError)
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
json.NewEncoder(w).Encode(map[string]interface{}{
"error": err.Error(),
})
@@ -59,6 +59,7 @@ type errorer interface {
// encode errors from business-logic
func encodeError(_ context.Context, err error, w http.ResponseWriter) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
switch err {
case cargo.ErrUnknown:
w.WriteHeader(http.StatusNotFound)
@@ -67,7 +68,6 @@ func encodeError(_ context.Context, err error, w http.ResponseWriter) {
default:
w.WriteHeader(http.StatusInternalServerError)
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
json.NewEncoder(w).Encode(map[string]interface{}{
"error": err.Error(),
})

0 comments on commit 8392035

Please sign in to comment.