Skip to content

Commit

Permalink
fix 恢复直接使用enc.SetEscapeHTML参数 (#412)
Browse files Browse the repository at this point in the history
Co-authored-by: zhenlinwen <[email protected]>
  • Loading branch information
silenceper and silenceper authored Jun 23, 2021
1 parent 45caf61 commit 5d8fd1f
Showing 1 changed file with 10 additions and 13 deletions.
23 changes: 10 additions & 13 deletions util/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down

0 comments on commit 5d8fd1f

Please sign in to comment.