fix(api): add json content-type to all json API responses (#1809)

pull/1812/head
Igor Karpovich 7 years ago committed by Anthony Lapenna
parent 4f0e752d00
commit 7690ef3c33

@ -17,6 +17,7 @@ func WriteErrorResponse(w http.ResponseWriter, err error, code int, logger *log.
logger.Printf("http error: %s (code=%d)", err, code) logger.Printf("http error: %s (code=%d)", err, code)
} }
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code) w.WriteHeader(code)
json.NewEncoder(w).Encode(&errorResponse{Err: err.Error()}) json.NewEncoder(w).Encode(&errorResponse{Err: err.Error()})
} }

@ -90,6 +90,7 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// encodeJSON encodes v to w in JSON format. WriteErrorResponse() is called if encoding fails. // encodeJSON encodes v to w in JSON format. WriteErrorResponse() is called if encoding fails.
func encodeJSON(w http.ResponseWriter, v interface{}, logger *log.Logger) { func encodeJSON(w http.ResponseWriter, v interface{}, logger *log.Logger) {
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(v); err != nil { if err := json.NewEncoder(w).Encode(v); err != nil {
httperror.WriteErrorResponse(w, err, http.StatusInternalServerError, logger) httperror.WriteErrorResponse(w, err, http.StatusInternalServerError, logger)
} }

Loading…
Cancel
Save