增加高级封装的request方法

This commit is contained in:
白茶清欢 2021-09-14 20:54:26 +08:00
parent 8d55472b3a
commit 60c07d090b

View File

@ -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请求参数