From 60c07d090bcc7157060f6d5a579098d8a9c0a296 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Tue, 14 Sep 2021 20:54:26 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=AB=98=E7=BA=A7=E5=B0=81?= =?UTF-8?q?=E8=A3=85=E7=9A=84request=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gin/request/util.go | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/gin/request/util.go b/gin/request/util.go index d4067c6..0e2ba36 100644 --- a/gin/request/util.go +++ b/gin/request/util.go @@ -9,6 +9,7 @@ package request import ( "encoding/json" + "net/http" "net/url" "strings" @@ -17,6 +18,20 @@ import ( "github.com/gin-gonic/gin" ) +// ParseRequestParam 解析请求数据 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 8:52 下午 2021/9/14 +func ParseRequestParam(ctx *gin.Context) (map[string]interface{}, error) { + switch ctx.Request.Method { + case http.MethodPost: + return ParsePostRequestBody(ctx) + default: + return ParseGetRequestBody(ctx) + } +} + // ParseGetRequestURI 解析URL请求参数 // // Author : go_developer@163.com<白茶清欢> @@ -60,8 +75,8 @@ func ParseGetRequestURI(url *url.URL) map[string][]string { // Author : zhangdeman001@ke.com<白茶清欢> // // Date : 9:07 下午 2021/7/24 -func ParseGetRequestBody(ctx *gin.Context) map[string]string { - result := make(map[string]string) +func ParseGetRequestBody(ctx *gin.Context) (map[string]interface{}, error) { + result := make(map[string]interface{}) urlRaw := strings.TrimLeft(ctx.Request.URL.RawQuery, "/") queryArr := strings.Split(urlRaw, "&") for _, itemPair := range queryArr { @@ -72,7 +87,7 @@ func ParseGetRequestBody(ctx *gin.Context) map[string]string { result[itemPairArr[0]] = strings.Join(itemPairArr[1:], "=") } } - return result + return result, nil } // ParsePostRequestBody 解析post请求参数