From 528eed24b11e2a2eff6e2dfa983520847abc8c62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CmacOS=E2=80=9D?= <“136255713@qq.com”> Date: Mon, 9 Oct 2023 12:22:13 +0800 Subject: [PATCH] =?UTF-8?q?utils/code.go=20=E5=A2=9E=E5=8A=A0Marshal?= =?UTF-8?q?=E6=96=B9=E6=B3=95;=20json=E8=AF=B7=E6=B1=82=E4=BD=93=E6=94=B9?= =?UTF-8?q?=E7=94=A8=E8=87=AA=E5=AE=9A=E4=B9=89Marshal=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- models/PrepareRequest.go | 3 +-- utils/code.go | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/models/PrepareRequest.go b/models/PrepareRequest.go index 1b264a4..b88b93e 100644 --- a/models/PrepareRequest.go +++ b/models/PrepareRequest.go @@ -3,7 +3,6 @@ package models import ( "bytes" "encoding/base64" - jsonp "encoding/json" "errors" "fmt" "github.com/wangluozhe/fhttp" @@ -139,7 +138,7 @@ func (pr *PrepareRequest) Prepare_body(data *url.Values, files *url.Files, json } if data == nil && json != nil { content_type = "application/json" - json_byte, err := jsonp.Marshal(json) + json_byte, err := utils.Marshal(json) if err != nil { return err } diff --git a/utils/code.go b/utils/code.go index 3fb01c2..76a8832 100644 --- a/utils/code.go +++ b/utils/code.go @@ -6,6 +6,7 @@ import ( "encoding/base64" "encoding/binary" "encoding/hex" + "encoding/json" "errors" "net/url" "regexp" @@ -179,3 +180,17 @@ func UnEscape(s interface{}) string { str = DecodeURIComponent(str) return str } + +// Marshal 避免json.Marshal对 "<", ">", "&" 等字符进行HTML编码 +func Marshal(data interface{}) ([]byte, error) { + var buffer bytes.Buffer + + encoder := json.NewEncoder(&buffer) + + // 禁用HTML转义 + encoder.SetEscapeHTML(false) + + err := encoder.Encode(data) + + return buffer.Bytes(), err +}