升级 wrapperHandle -> ParseBody

This commit is contained in:
白茶清欢 2024-11-04 16:12:09 +08:00
parent a0b4cdb414
commit f6db9e8edb

View File

@ -8,11 +8,9 @@
package request
import (
"bytes"
"git.zhangdeman.cn/zhangdeman/consts"
"git.zhangdeman.cn/zhangdeman/gin/request/parse_body"
"git.zhangdeman.cn/zhangdeman/wrapper"
"github.com/gin-gonic/gin"
"io"
"strings"
)
@ -140,28 +138,8 @@ func (wh *wrapperHandle) GetDomain(ctx *gin.Context) string {
// Date : 11:25 2024/7/26
func (wh *wrapperHandle) ParseBody(ctx *gin.Context) map[string]any {
body := map[string]any{}
contentType := wh.GetContentType(ctx, "")
if strings.Contains(contentType, consts.MimeTypeXWWWFormUrlencoded) { // form请求
if err := ctx.Request.ParseForm(); nil != err {
return body
}
for paramName, itemParam := range ctx.Request.PostForm {
if len(itemParam) > 0 {
body[paramName] = itemParam[0]
} else {
body[paramName] = ""
}
}
return body
}
if strings.Contains(contentType, consts.MimeTypeJson) || nil != ctx.Request.Body { // json请求 或者非 json请求 存在 body
data, err := io.ReadAll(ctx.Request.Body)
if nil != err {
return body
}
ctx.Request.Body = io.NopCloser(bytes.NewBuffer(data))
return body
if _, err := parse_body.Execute(ctx, &body); nil != err {
return map[string]any{}
}
return body