增加post解析application/json请求体body

This commit is contained in:
白茶清欢 2021-09-14 20:08:26 +08:00
parent 635028350e
commit 1e97af42b9

View File

@ -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
}