From 823ab61e0f1eb0a9ce6aa786f0d01621b5e92da2 Mon Sep 17 00:00:00 2001 From: hbc Date: Fri, 26 May 2017 10:10:52 +0800 Subject: [PATCH] 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 --- shipping/booking/transport.go | 2 +- shipping/handling/transport.go | 2 +- shipping/tracking/transport.go | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/shipping/booking/transport.go b/shipping/booking/transport.go index 6592a9d1b..8cf11a26c 100644 --- a/shipping/booking/transport.go +++ b/shipping/booking/transport.go @@ -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(), }) diff --git a/shipping/handling/transport.go b/shipping/handling/transport.go index 065bda1d2..0e21365ba 100644 --- a/shipping/handling/transport.go +++ b/shipping/handling/transport.go @@ -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(), }) diff --git a/shipping/tracking/transport.go b/shipping/tracking/transport.go index 13d17f25c..32db97167 100644 --- a/shipping/tracking/transport.go +++ b/shipping/tracking/transport.go @@ -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(), })