From 1e97af42b97e71c51938dce8019be6adf634d0b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Tue, 14 Sep 2021 20:08:26 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0post=E8=A7=A3=E6=9E=90applica?= =?UTF-8?q?tion/json=E8=AF=B7=E6=B1=82=E4=BD=93body?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gin/request/util.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) 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 +}