From 7c4b8fe1724cbd3866e87479d5af7a5ec9c41082 Mon Sep 17 00:00:00 2001 From: silenceper Date: Mon, 13 Feb 2017 00:25:14 +0800 Subject: [PATCH 1/2] =?UTF-8?q?json=20marshal=E6=94=B9=E4=B8=BAencode=20?= =?UTF-8?q?=EF=BC=8C=E4=B8=8D=E8=BD=AC=E4=B9=89html=E5=AD=97=E7=AC=A6(?= =?UTF-8?q?=E4=BE=8B=E5=A6=82=EF=BC=9A&)=20#15?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- util/http.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/util/http.go b/util/http.go index 05f9d29..151a41d 100644 --- a/util/http.go +++ b/util/http.go @@ -27,12 +27,16 @@ func HTTPGet(uri 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 } - 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 } From f37e318f054c435a3c81fd27cd1df32fbebef66f Mon Sep 17 00:00:00 2001 From: silenceper Date: Mon, 13 Feb 2017 00:38:02 +0800 Subject: [PATCH 2/2] =?UTF-8?q?enc.SetEscapeHTML=20=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E7=89=88=E6=9C=AC>=3D1.7=EF=BC=8C=E4=BD=BF=E7=94=A8=E7=AE=80?= =?UTF-8?q?=E5=8D=95=E6=9B=BF=E6=8D=A2=20#15?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- util/http.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/util/http.go b/util/http.go index 151a41d..ca98ae7 100644 --- a/util/http.go +++ b/util/http.go @@ -27,16 +27,17 @@ func HTTPGet(uri string) ([]byte, error) { //PostJSON post json 数据请求 func PostJSON(uri string, obj interface{}) ([]byte, error) { - jsonBuf := new(bytes.Buffer) - enc := json.NewEncoder(jsonBuf) - enc.SetEscapeHTML(false) - err := enc.Encode(obj) - + jsonData, err := json.Marshal(obj) if err != nil { return nil, err } - response, err := http.Post(uri, "application/json;charset=utf-8", jsonBuf) + jsonData = bytes.Replace(jsonData, []byte("\\u003c"), []byte("<"), -1) + jsonData = bytes.Replace(jsonData, []byte("\\u003e"), []byte(">"), -1) + jsonData = bytes.Replace(jsonData, []byte("\\u0026"), []byte("&"), -1) + + body := bytes.NewBuffer(jsonData) + response, err := http.Post(uri, "application/json;charset=utf-8", body) if err != nil { return nil, err }