修复parse_body相关BUG

This commit is contained in:
白茶清欢 2025-05-04 14:52:20 +08:00
parent 2aa8e86917
commit 927870b11a
3 changed files with 9 additions and 11 deletions

View File

@ -8,6 +8,7 @@
package middleware package middleware
import ( import (
"git.zhangdeman.cn/zhangdeman/gin/request/parse_body"
"strings" "strings"
"git.zhangdeman.cn/zhangdeman/consts" "git.zhangdeman.cn/zhangdeman/consts"
@ -97,12 +98,14 @@ func LogRequest(cfg *AccessConfig) gin.HandlerFunc {
} }
startRequestTime := request.WrapperHandle.GetCtxIntData(ctx, handleConfig.StartRequestTimeField, 0) startRequestTime := request.WrapperHandle.GetCtxIntData(ctx, handleConfig.StartRequestTimeField, 0)
// 记录请求日志 // 记录请求日志
logData := logger.NewLogData(ctx, consts.LogTypeRequest, "", map[string]any{ data := map[string]any{
handleConfig.StartRequestTimeField: startRequestTime, // 开始请求时间 handleConfig.StartRequestTimeField: startRequestTime, // 开始请求时间
"request_header": getLogRequestHeader(ctx, cfg), // 请求header "request_header": getLogRequestHeader(ctx, cfg), // 请求header
"request_query": request.WrapperHandle.ParseQuery(ctx), // 获取请求query "request_query": request.WrapperHandle.ParseQuery(ctx), // 获取请求query
"request_body": request.WrapperHandle.ParseBody(ctx), // 请求body "request_body": map[string]any{}, // 请求body
}) }
data["request_body"], _ = parse_body.ExecuteForMap(ctx)
logData := logger.NewLogData(ctx, consts.LogTypeRequest, "", data)
cfg.Logger.Info("接口请求日志记录", logger.ZapLogDataList(logData)...) cfg.Logger.Info("接口请求日志记录", logger.ZapLogDataList(logData)...)
ctx.Next() ctx.Next()
// 结束时间 // 结束时间

View File

@ -46,7 +46,7 @@ func Register(requestType string, adaptor abstract.RequestBodyParseAdaptor) {
func Execute(ctx *gin.Context, receiver any) ([]byte, error) { func Execute(ctx *gin.Context, receiver any) ([]byte, error) {
contentType := strings.ToLower(strings.ReplaceAll(ctx.ContentType(), " ", "")) contentType := strings.ToLower(strings.ReplaceAll(ctx.ContentType(), " ", ""))
if len(contentType) == 0 { if len(contentType) == 0 {
return nil, errors.New("content_type is empty") return []byte("{}"), nil
} }
// 裁剪出真实的类型,之所以截取,是因为 content_type 中可能还包含编码信息, 如 : application/json;chaset=utf8 // 裁剪出真实的类型,之所以截取,是因为 content_type 中可能还包含编码信息, 如 : application/json;chaset=utf8
contentTypeArr := strings.Split(contentType, ";") contentTypeArr := strings.Split(contentType, ";")

View File

@ -141,13 +141,8 @@ func (wh *wrapperHandle) GetDomain(ctx *gin.Context) string {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// 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, error) {
body := map[string]any{} return parse_body.ExecuteForMap(ctx)
if _, err := parse_body.Execute(ctx, &body); nil != err {
return map[string]any{}
}
return body
} }
// GetResponseBody 获取响应body // GetResponseBody 获取响应body