From c52df773ac6ef0835e0346380918a4813bec4de2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Wed, 15 Sep 2021 10:41:13 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90formurlencode=E5=8F=82?= =?UTF-8?q?=E6=95=B0=E8=A7=A3=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gin/request/util.go | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/gin/request/util.go b/gin/request/util.go index 0e2ba36..60460e0 100644 --- a/gin/request/util.go +++ b/gin/request/util.go @@ -9,6 +9,7 @@ package request import ( "encoding/json" + "io" "net/http" "net/url" "strings" @@ -81,6 +82,9 @@ func ParseGetRequestBody(ctx *gin.Context) (map[string]interface{}, error) { queryArr := strings.Split(urlRaw, "&") for _, itemPair := range queryArr { itemPairArr := strings.Split(itemPair, "=") + if len(itemPairArr) == 0 { + continue + } if len(itemPairArr) == 1 { result[itemPairArr[0]] = "" } else { @@ -139,16 +143,38 @@ func ParsePostRequestForApplicationJSON(ctx *gin.Context) (map[string]interface{ // Date : 8:11 下午 2021/9/14 func ParsePostRequestForApplicationFormURLEncoded(ctx *gin.Context) (map[string]interface{}, error) { var ( - err error - result map[string]interface{} + err error + result map[string]interface{} + byteData []byte ) - if err = ctx.Request.ParseForm(); nil != err { + if byteData, err = io.ReadAll(ctx.Request.Body); nil != err { return make(map[string]interface{}), err } - if err = util.JSONUnmarshalWithNumberForIOReader(ctx.Request.Body, &result); nil != err { - return make(map[string]interface{}), err + + tmpResult := make(map[string][]string) + orgArr := strings.Split(string(byteData), "&") + for _, kvPair := range orgArr { + kv := strings.Split(kvPair, "=") + if len(kv) == 0 { + continue + } + + if _, exist := tmpResult[kv[0]]; !exist { + tmpResult[kv[0]] = make([]string, 0) + } + if len(kv) == 1 { + tmpResult[kv[0]] = append(tmpResult[kv[0]], "") + } else { + tmpResult[kv[0]] = append(tmpResult[kv[0]], strings.Join(kv[1:], "=")) + } } + + result = make(map[string]interface{}) + for k, v := range tmpResult { + result[k] = v + } + return result, err }