diff --git a/gin/request/util.go b/gin/request/util.go index 2a85013..1ca7a73 100644 --- a/gin/request/util.go +++ b/gin/request/util.go @@ -8,6 +8,7 @@ package request import ( + "encoding/json" "net/url" "strings" @@ -71,3 +72,35 @@ func ParseGetRequestBody(ctx *gin.Context) map[string]string { } return result } + +// ParsePostRequestBody 解析post请求参数 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 7:38 下午 2021/9/14 +func ParsePostRequestBody(ctx *gin.Context) (map[string]interface{}, error) { + contentType := strings.ToLower(ctx.ContentType()) + if strings.Contains(contentType, "application/json") { + // application/json请求参数 + return ParsePostRequestForApplicationJSON(ctx) + } + return make(map[string]interface{}), nil +} + +// ParsePostRequestForApplicationJSON 解析application/json请求 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 8:00 下午 2021/9/14 +func ParsePostRequestForApplicationJSON(ctx *gin.Context) (map[string]interface{}, error) { + var ( + err error + result map[string]interface{} + ) + decoder := json.NewDecoder(ctx.Request.Body) + decoder.UseNumber() + if err = decoder.Decode(&result); nil != err { + return make(map[string]interface{}), err + } + return result, nil +}