升级请求体解析 #1
@ -8,11 +8,9 @@
|
|||||||
package request
|
package request
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"git.zhangdeman.cn/zhangdeman/gin/request/parse_body"
|
||||||
"git.zhangdeman.cn/zhangdeman/consts"
|
|
||||||
"git.zhangdeman.cn/zhangdeman/wrapper"
|
"git.zhangdeman.cn/zhangdeman/wrapper"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"io"
|
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -140,28 +138,8 @@ func (wh *wrapperHandle) GetDomain(ctx *gin.Context) string {
|
|||||||
// Date : 11:25 2024/7/26
|
// Date : 11:25 2024/7/26
|
||||||
func (wh *wrapperHandle) ParseBody(ctx *gin.Context) map[string]any {
|
func (wh *wrapperHandle) ParseBody(ctx *gin.Context) map[string]any {
|
||||||
body := map[string]any{}
|
body := map[string]any{}
|
||||||
contentType := wh.GetContentType(ctx, "")
|
if _, err := parse_body.Execute(ctx, &body); nil != err {
|
||||||
if strings.Contains(contentType, consts.MimeTypeXWWWFormUrlencoded) { // form请求
|
return map[string]any{}
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return body
|
return body
|
||||||
|
Loading…
Reference in New Issue
Block a user