完成formurlencode参数解析

This commit is contained in:
白茶清欢 2021-09-15 10:41:13 +08:00
parent 60c07d090b
commit c52df773ac

View File

@ -9,6 +9,7 @@ package request
import ( import (
"encoding/json" "encoding/json"
"io"
"net/http" "net/http"
"net/url" "net/url"
"strings" "strings"
@ -81,6 +82,9 @@ func ParseGetRequestBody(ctx *gin.Context) (map[string]interface{}, error) {
queryArr := strings.Split(urlRaw, "&") queryArr := strings.Split(urlRaw, "&")
for _, itemPair := range queryArr { for _, itemPair := range queryArr {
itemPairArr := strings.Split(itemPair, "=") itemPairArr := strings.Split(itemPair, "=")
if len(itemPairArr) == 0 {
continue
}
if len(itemPairArr) == 1 { if len(itemPairArr) == 1 {
result[itemPairArr[0]] = "" result[itemPairArr[0]] = ""
} else { } else {
@ -141,14 +145,36 @@ func ParsePostRequestForApplicationFormURLEncoded(ctx *gin.Context) (map[string]
var ( var (
err error err error
result map[string]interface{} 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 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 return result, err
} }