REST API输出时增加application/json; charset=utf-8 Header

This commit is contained in:
GoEdgeLab
2021-06-16 10:48:31 +08:00
parent 3a25fea374
commit df409f6b11

View File

@@ -147,7 +147,7 @@ func (this *RestServer) handle(writer http.ResponseWriter, req *http.Request) {
err = json.Unmarshal(body, reqValue) err = json.Unmarshal(body, reqValue)
if err != nil { if err != nil {
writer.WriteHeader(http.StatusBadRequest) writer.WriteHeader(http.StatusBadRequest)
_, _ = writer.Write([]byte(err.Error())) _, _ = writer.Write([]byte("Decode request failed: " + err.Error() + ". Request body should be a valid JSON data"))
return return
} }
@@ -187,13 +187,15 @@ func (this *RestServer) handle(writer http.ResponseWriter, req *http.Request) {
"data": maps.Map{}, "data": maps.Map{},
}, shouldPretty) }, shouldPretty)
} else { } else {
writer.Header().Set("Content-Type", "application/json; charset=utf-8")
_, _ = writer.Write(dataJSON) _, _ = writer.Write(dataJSON)
} }
} }
} }
func (this *RestServer) writeJSON(writer http.ResponseWriter, v maps.Map, pretty bool) { func (this *RestServer) writeJSON(writer http.ResponseWriter, v maps.Map, pretty bool) {
writer.Header().Set("Content-Type", "application/json") writer.Header().Set("Content-Type", "application/json; charset=utf-8")
if pretty { if pretty {
_, _ = writer.Write(v.AsPrettyJSON()) _, _ = writer.Write(v.AsPrettyJSON())