From b2fe05111c2d7dc95b98ec3a66ee602600b3a4ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Fri, 26 Jul 2024 11:52:53 +0800 Subject: [PATCH] fix --- request/wrapper.go | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/request/wrapper.go b/request/wrapper.go index df846f7..689f1e1 100644 --- a/request/wrapper.go +++ b/request/wrapper.go @@ -13,7 +13,6 @@ import ( "git.zhangdeman.cn/zhangdeman/wrapper" "github.com/gin-gonic/gin" "io" - "net/url" "strings" ) @@ -85,7 +84,7 @@ func (wh *wrapperHandle) GetScheme(ctx *gin.Context, defaultVal string) string { func (wh *wrapperHandle) GetQuery(ctx *gin.Context) map[string]string { query := make(map[string]string) if nil != ctx && nil != ctx.Request && nil != ctx.Request.URL { - for paramName, valueList := range { + for paramName, valueList := range ctx.Request.URL.Query() { if len(valueList) == 0 { query[paramName] = "" } else { @@ -130,14 +129,6 @@ func (wh *wrapperHandle) GetContentType(ctx *gin.Context, defaultVal string) str func (wh *wrapperHandle) GetRequestBody(ctx *gin.Context) map[string]any { body := map[string]any{} contentType := wh.GetContentType(ctx, "") - if strings.Contains(contentType, consts.MimeTypeJson) { // json请求 - data, err := io.ReadAll(ctx.Request.Body) - if nil != err { - return body - } - ctx.Request.Body = io.NopCloser(bytes.NewBuffer(data)) - return body - } if strings.Contains(contentType, consts.MimeTypeXWWWFormUrlencoded) { // form请求 if err := ctx.Request.ParseForm(); nil != err { return body @@ -151,6 +142,16 @@ func (wh *wrapperHandle) GetRequestBody(ctx *gin.Context) map[string]any { } return body } + + if strings.Contains(contentType, consts.MimeTypeJson) || nil != ctx.Request.Body { // json请求 或者非 json请求 存在 body + data, err := io.ReadAll(ctx.Request.Body) + if nil != err { + return body + } + ctx.Request.Body = io.NopCloser(bytes.NewBuffer(data)) + return body + } + return body }