Просмотр исходного кода

fix 恢复直接使用enc.SetEscapeHTML参数 (#412)

Co-authored-by: zhenlinwen <zhenlinwen@tencent.com>
silenceper 5 лет назад
Родитель
Сommit
5d8fd1f5bd
1 измененных файлов с 10 добавлено и 13 удалено
  1. 10 13
      util/http.go

+ 10 - 13
util/http.go

@@ -48,15 +48,14 @@ func HTTPPost(uri string, data string) ([]byte, error) {
 
 
 //PostJSON post json 数据请求
 //PostJSON post json 数据请求
 func PostJSON(uri string, obj interface{}) ([]byte, error) {
 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 {
 	if err != nil {
 		return nil, err
 		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 {
 	if err != nil {
 		return nil, err
 		return nil, err
 	}
 	}
@@ -70,17 +69,15 @@ func PostJSON(uri string, obj interface{}) ([]byte, error) {
 
 
 // PostJSONWithRespContentType post json数据请求,且返回数据类型
 // PostJSONWithRespContentType post json数据请求,且返回数据类型
 func PostJSONWithRespContentType(uri string, obj interface{}) ([]byte, string, error) {
 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 {
 	if err != nil {
 		return nil, "", err
 		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 {
 	if err != nil {
 		return nil, "", err
 		return nil, "", err
 	}
 	}