feat: 优化Body参数,支持any类型

This commit is contained in:
2025-12-15 12:33:30 +08:00
parent a2b6122712
commit c71b4aabbb
2 changed files with 39 additions and 11 deletions

View File

@ -21,7 +21,7 @@ import (
type Request struct { type Request struct {
Ctx context.Context `json:"-"` // 请求上下文 Ctx context.Context `json:"-"` // 请求上下文
PathParam map[string]string `json:"path_param"` // 替换 url 中的占位符 PathParam map[string]string `json:"path_param"` // 替换 url 中的占位符
Body map[string]any `json:"body"` // 请求Body Body any `json:"body"` // 请求 Body
Header map[string]any `json:"header"` // 请求 Header Header map[string]any `json:"header"` // 请求 Header
Cookie map[string]any `json:"cookie"` // 请求 Cookie Cookie map[string]any `json:"cookie"` // 请求 Cookie
Query map[string]any `json:"query"` // 请求 query Query map[string]any `json:"query"` // 请求 query

View File

@ -10,6 +10,7 @@ package httpclient
import ( import (
"errors" "errors"
"fmt" "fmt"
"io"
"net/http" "net/http"
"net/textproto" "net/textproto"
"strings" "strings"
@ -17,6 +18,7 @@ import (
"git.zhangdeman.cn/zhangdeman/consts" "git.zhangdeman.cn/zhangdeman/consts"
"git.zhangdeman.cn/zhangdeman/network/httpclient/define" "git.zhangdeman.cn/zhangdeman/network/httpclient/define"
requestBoodyWriter "git.zhangdeman.cn/zhangdeman/network/httpclient/implement/request" requestBoodyWriter "git.zhangdeman.cn/zhangdeman/network/httpclient/implement/request"
"git.zhangdeman.cn/zhangdeman/serialize"
"git.zhangdeman.cn/zhangdeman/wrapper/op_any" "git.zhangdeman.cn/zhangdeman/wrapper/op_any"
"resty.dev/v3" "resty.dev/v3"
) )
@ -42,12 +44,32 @@ func initRequestConfig(reqConfig *define.Request) {
reqConfig.PathParam = map[string]string{} reqConfig.PathParam = map[string]string{}
} }
// body 转为 map
bodyMap := make(map[string]any)
if nil == reqConfig.Body {
bodyMap = map[string]any{}
} else {
switch val := reqConfig.Body.(type) {
case map[string]any:
bodyMap = val
case struct{}, *struct{}:
serialize.JSON.TransitionIgnoreError(reqConfig.Body, &bodyMap)
case string:
serialize.JSON.UnmarshalWithNumberIgnoreError([]byte(val), &bodyMap)
case []byte:
serialize.JSON.UnmarshalWithNumberIgnoreError(val, &bodyMap)
case io.Reader:
dataContent, _ := io.ReadAll(val)
serialize.JSON.UnmarshalWithNumberIgnoreError(dataContent, &bodyMap)
}
}
// 合并静态参数, 传入参数与静态参数存在相同参数, 以静态参数为准 // 合并静态参数, 传入参数与静态参数存在相同参数, 以静态参数为准
for loc, paramTable := range reqConfig.Static { for loc, paramTable := range reqConfig.Static {
switch strings.ToUpper(loc) { switch strings.ToUpper(loc) {
case consts.RequestDataLocationBody.String(): case consts.RequestDataLocationBody.String():
for name, val := range paramTable { for name, val := range paramTable {
reqConfig.Body[name] = val bodyMap[name] = val
} }
case consts.RequestDataLocationQuery.String(): case consts.RequestDataLocationQuery.String():
for name, val := range paramTable { for name, val := range paramTable {
@ -68,6 +90,9 @@ func initRequestConfig(reqConfig *define.Request) {
} }
} }
// 重置 Body
reqConfig.Body = bodyMap
formatHeader(reqConfig) formatHeader(reqConfig)
} }
@ -78,6 +103,9 @@ func initRequestConfig(reqConfig *define.Request) {
// Date : 15:00 2024/5/31 // Date : 15:00 2024/5/31
func NewRestyClient(reqConfig *define.Request, reqOption *RequestOption) (*resty.Client, *resty.Request, error) { func NewRestyClient(reqConfig *define.Request, reqOption *RequestOption) (*resty.Client, *resty.Request, error) {
client := resty.New() client := resty.New()
defer func() {
_ = client.Close()
}()
request := client.R() request := client.R()
if nil == reqConfig { if nil == reqConfig {
return nil, nil, errors.New("request config is nil") return nil, nil, errors.New("request config is nil")
@ -149,11 +177,11 @@ func setRestyBody(reqConfig *define.Request, requestOption *RequestOption, reque
} }
if nil != requestOption && nil != requestOption.RequestBodyWrite { if nil != requestOption && nil != requestOption.RequestBodyWrite {
// 外部传入的实现 // 外部传入的实现
requestOption.RequestBodyWrite.Write(request, reqConfig.Body) requestOption.RequestBodyWrite.Write(request, reqConfig.Body.(map[string]any))
return nil return nil
} }
// 外部没传入, 使用内置实现, 内置默认实现支持: xml/json/form, 如需其他, 自行扩展 // 外部没传入, 使用内置实现, 内置默认实现支持: xml/json/form, 如需其他, 自行扩展
return requestBoodyWriter.WriteBody(reqConfig.ContentType, request, reqConfig.Body) return requestBoodyWriter.WriteBody(reqConfig.ContentType, request, reqConfig.Body.(map[string]any))
} }
// formatHeader 格式化header // formatHeader 格式化header