From 2bd8cf162fc9a5efbe626f9a514b320eb8a785dc Mon Sep 17 00:00:00 2001 From: jjxu <428192774@qq.com> Date: Sat, 14 Sep 2024 14:02:33 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E8=AF=B7=E6=B1=82?= =?UTF-8?q?=E4=BD=93=E4=B8=BAnil=E6=97=B6=E8=AF=BB=E5=8F=96=E6=8A=A5?= =?UTF-8?q?=E9=94=99=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- simpleRequest.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/simpleRequest.go b/simpleRequest.go index 6695b22..3e59dbe 100644 --- a/simpleRequest.go +++ b/simpleRequest.go @@ -54,7 +54,7 @@ type SimpleRequest struct { BodyEntries map[string]any // 输入的body中的内容 bodyEntryParsers map[string]IBodyEntryParser // 用于将BodyEntries中的内容解析后传入request body中 disableDefaultContentType bool // 禁用默认的ContentType - disableCopyRequestBody bool // 禁用默认的ContentType,在进行http请求后SimpleRequest.Request.Body中内容会无法读取 + disableCopyRequestBody bool // 禁用复制请求体,在进行http请求后SimpleRequest.Request.Body中内容会无法读取,但是会提高性能 timeout time.Duration @@ -173,12 +173,15 @@ func (s *SimpleRequest) GET(urls string) (body []byte, err error) { // 通用的请求方法 func (s *SimpleRequest) LaunchTo(urls, method string) (body []byte, err error) { + var r *http.Request // body s.initBody() - var r *http.Request - copBody, err := io.ReadAll(s.body) - if err != nil { - return + var copBody []byte + if s.body != nil { + copBody, err = io.ReadAll(s.body) + if err != nil { + return + } } if !s.disableCopyRequestBody { // 使r.body在请求后仍旧可读,便于使用者对请求过程进行分析