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 }