From 5d8fd1f5bdde54a9fb68d29cd4d0072606c68fd9 Mon Sep 17 00:00:00 2001 From: silenceper Date: Wed, 23 Jun 2021 09:49:41 +0800 Subject: [PATCH] =?UTF-8?q?fix=20=E6=81=A2=E5=A4=8D=E7=9B=B4=E6=8E=A5?= =?UTF-8?q?=E4=BD=BF=E7=94=A8enc.SetEscapeHTML=E5=8F=82=E6=95=B0=20(#412)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: zhenlinwen --- util/http.go | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/util/http.go b/util/http.go index 56d1650..dd2788e 100644 --- a/util/http.go +++ b/util/http.go @@ -48,15 +48,14 @@ func HTTPPost(uri string, data string) ([]byte, error) { //PostJSON post json 数据请求 func PostJSON(uri string, obj interface{}) ([]byte, error) { - jsonData, err := json.Marshal(obj) + jsonBuf := new(bytes.Buffer) + enc := json.NewEncoder(jsonBuf) + enc.SetEscapeHTML(false) + err := enc.Encode(obj) if err != nil { return nil, err } - jsonData = bytes.ReplaceAll(jsonData, []byte("\\u003c"), []byte("<")) - jsonData = bytes.ReplaceAll(jsonData, []byte("\\u003e"), []byte(">")) - jsonData = bytes.ReplaceAll(jsonData, []byte("\\u0026"), []byte("&")) - body := bytes.NewBuffer(jsonData) - response, err := http.Post(uri, "application/json;charset=utf-8", body) + response, err := http.Post(uri, "application/json;charset=utf-8", jsonBuf) if err != nil { return nil, err } @@ -70,17 +69,15 @@ func PostJSON(uri string, obj interface{}) ([]byte, error) { // PostJSONWithRespContentType post json数据请求,且返回数据类型 func PostJSONWithRespContentType(uri string, obj interface{}) ([]byte, string, error) { - jsonData, err := json.Marshal(obj) + jsonBuf := new(bytes.Buffer) + enc := json.NewEncoder(jsonBuf) + enc.SetEscapeHTML(false) + err := enc.Encode(obj) if err != nil { return nil, "", err } - jsonData = bytes.ReplaceAll(jsonData, []byte("\\u003c"), []byte("<")) - jsonData = bytes.ReplaceAll(jsonData, []byte("\\u003e"), []byte(">")) - jsonData = bytes.ReplaceAll(jsonData, []byte("\\u0026"), []byte("&")) - - body := bytes.NewBuffer(jsonData) - response, err := http.Post(uri, "application/json;charset=utf-8", body) + response, err := http.Post(uri, "application/json;charset=utf-8", jsonBuf) if err != nil { return nil, "", err }