升级请求体解析 #1

Merged
zhangdeman merged 8 commits from feature/parse_body into master 2024-11-05 17:32:44 +08:00
Showing only changes of commit f6db9e8edb - Show all commits

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