支持自适应请求类型, 根据不同请求类型设置正确Body格式

This commit is contained in:
2025-05-08 15:12:37 +08:00
parent 1abe6c1c29
commit 1403693fda
9 changed files with 203 additions and 30 deletions

View File

@ -8,12 +8,12 @@
package httpclient
import (
"errors"
"fmt"
"git.zhangdeman.cn/zhangdeman/consts"
"git.zhangdeman.cn/zhangdeman/network/httpclient/define"
"git.zhangdeman.cn/zhangdeman/serialize"
requestBoodyWriter "git.zhangdeman.cn/zhangdeman/network/httpclient/implement/request"
"git.zhangdeman.cn/zhangdeman/wrapper"
"github.com/tidwall/gjson"
"net/http"
"net/textproto"
"resty.dev/v3"
@ -75,11 +75,11 @@ func initRequestConfig(reqConfig *define.Request) {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:00 2024/5/31
func NewRestyClient(reqConfig *define.Request, reqOption *RequestOption) (*resty.Client, *resty.Request) {
func NewRestyClient(reqConfig *define.Request, reqOption *RequestOption) (*resty.Client, *resty.Request, error) {
client := resty.New()
request := client.R()
if nil == reqConfig {
return client, request
return nil, nil, errors.New("request config is nil")
}
// 限流处理, 增加限流中间件
client.AddRequestMiddleware(func(client *resty.Client, request *resty.Request) error {
@ -93,7 +93,7 @@ func NewRestyClient(reqConfig *define.Request, reqOption *RequestOption) (*resty
}
return nil
})
initRequestConfig(reqConfig) // 初始化 + 格式化配置
client.SetAllowMethodGetPayload(true) // 配置 GET 请求允许带 Body
client.SetAllowMethodDeletePayload(true) // 配置 DELETE 请求允许带 Body
@ -130,35 +130,29 @@ func NewRestyClient(reqConfig *define.Request, reqOption *RequestOption) (*resty
Value: wrapper.AnyDataType(cookieValue).ToString().Value(),
})
}
request.SetCookies(cookieList) // 设置cookie
setRestyBody(reqConfig, request) // 设置请求Body
return client, request
request.SetCookies(cookieList) // 设置cookie
if err := setRestyBody(reqConfig, reqOption, request); nil != err {
return nil, nil, err
}
return client, request, nil
}
// setRestyBody 设置请求BODY TODO: 支持xml / yml等
// setRestyBody 设置请求BODY
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:18 2024/5/31
func setRestyBody(reqConfig *define.Request, request *resty.Request) {
if nil == reqConfig.Body || len(reqConfig.Body) == 0 {
return
func setRestyBody(reqConfig *define.Request, requestOption *RequestOption, request *resty.Request) error {
if nil == reqConfig.Body {
return nil
}
if strings.Contains(strings.ToLower(reqConfig.ContentType), consts.MimeTypeJson) {
request.SetBody(reqConfig.Body)
return
if nil != requestOption && nil != requestOption.RequestBodyWrite {
// 外部传入的实现
requestOption.RequestBodyWrite.Write(request, reqConfig.Body)
return nil
}
if strings.Contains(strings.ToLower(reqConfig.ContentType), consts.MimeTypeXWWWFormUrlencoded) {
bodyStr := serialize.JSON.MarshalForStringIgnoreError(reqConfig.Body)
bodyData := map[string]string{}
jsonObj := gjson.Parse(bodyStr)
jsonObj.ForEach(func(key, value gjson.Result) bool {
bodyData[key.String()] = value.String()
return true
})
request.SetFormData(bodyData)
}
return
// 外部没传入, 使用内置实现, 内置默认实现支持: xml/json/form, 如需其他, 自行扩展
return requestBoodyWriter.WriteBody(reqConfig.ContentType, request, reqConfig.Body)
}
// formatHeader 格式化header