修复一波不兼容逻辑
This commit is contained in:
parent
17703d2521
commit
f14401a39f
@ -12,6 +12,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"
|
||||||
"git.zhangdeman.cn/zhangdeman/serialize"
|
"git.zhangdeman.cn/zhangdeman/serialize"
|
||||||
|
"git.zhangdeman.cn/zhangdeman/wrapper"
|
||||||
"github.com/go-resty/resty/v2"
|
"github.com/go-resty/resty/v2"
|
||||||
"github.com/tidwall/gjson"
|
"github.com/tidwall/gjson"
|
||||||
"net/http"
|
"net/http"
|
||||||
@ -37,8 +38,22 @@ func NewRestyClient(reqConfig *define.Request) (*resty.Client, *resty.Request) {
|
|||||||
client.SetJSONUnmarshaler(serialize.JSON.UnmarshalWithNumber) // 反序列化方法
|
client.SetJSONUnmarshaler(serialize.JSON.UnmarshalWithNumber) // 反序列化方法
|
||||||
|
|
||||||
request.SetPathParams(reqConfig.PathParam) // 设置path中的参数
|
request.SetPathParams(reqConfig.PathParam) // 设置path中的参数
|
||||||
request.SetQueryParams(reqConfig.Query) // 设置query参数
|
query := map[string]string{}
|
||||||
request.SetHeaders(reqConfig.Header) // 设置header
|
for queryName, queryValue := range reqConfig.Query {
|
||||||
|
if nil == queryValue {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
query[queryName] = wrapper.AnyDataType(queryValue).ToString().Value()
|
||||||
|
}
|
||||||
|
request.SetQueryParams(query) // 设置query参数
|
||||||
|
header := map[string]string{}
|
||||||
|
for headerName, headerValue := range reqConfig.Header {
|
||||||
|
if nil == headerValue {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
header[headerName] = wrapper.AnyDataType(headerValue).ToString().Value()
|
||||||
|
}
|
||||||
|
request.SetHeaders(header) // 设置header
|
||||||
request.URL = reqConfig.FullUrl // 请求接口
|
request.URL = reqConfig.FullUrl // 请求接口
|
||||||
for pathParamName, pathParamValue := range reqConfig.PathParam {
|
for pathParamName, pathParamValue := range reqConfig.PathParam {
|
||||||
if len(pathParamValue) == 0 {
|
if len(pathParamValue) == 0 {
|
||||||
@ -49,9 +64,12 @@ func NewRestyClient(reqConfig *define.Request) (*resty.Client, *resty.Request) {
|
|||||||
request.Method = reqConfig.Method // 请求方法
|
request.Method = reqConfig.Method // 请求方法
|
||||||
cookieList := make([]*http.Cookie, 0)
|
cookieList := make([]*http.Cookie, 0)
|
||||||
for cookieName, cookieValue := range reqConfig.Cookie {
|
for cookieName, cookieValue := range reqConfig.Cookie {
|
||||||
|
if nil == cookieValue {
|
||||||
|
continue
|
||||||
|
}
|
||||||
cookieList = append(cookieList, &http.Cookie{
|
cookieList = append(cookieList, &http.Cookie{
|
||||||
Name: cookieName,
|
Name: cookieName,
|
||||||
Value: cookieValue,
|
Value: wrapper.AnyDataType(cookieValue).ToString().Value(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
request.SetCookies(cookieList) // 设置cookie
|
request.SetCookies(cookieList) // 设置cookie
|
||||||
@ -94,8 +112,9 @@ func formatHeader(requestConfig *define.Request) {
|
|||||||
if nil == requestConfig {
|
if nil == requestConfig {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
formatHeaderData := make(map[string]string)
|
formatHeaderData := make(map[string]any)
|
||||||
for headerName, headerVal := range requestConfig.Header {
|
for headerName, headerVal := range requestConfig.Header {
|
||||||
|
// 规范化处理 header 名称
|
||||||
formatHeaderData[textproto.CanonicalMIMEHeaderKey(headerName)] = headerVal
|
formatHeaderData[textproto.CanonicalMIMEHeaderKey(headerName)] = headerVal
|
||||||
}
|
}
|
||||||
requestConfig.Header = formatHeaderData
|
requestConfig.Header = formatHeaderData
|
||||||
|
@ -8,8 +8,9 @@
|
|||||||
package validate
|
package validate
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"git.zhangdeman.cn/zhangdeman/network/httpclient/define"
|
"fmt"
|
||||||
"git.zhangdeman.cn/zhangdeman/consts"
|
"git.zhangdeman.cn/zhangdeman/consts"
|
||||||
|
"git.zhangdeman.cn/zhangdeman/network/httpclient/define"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
@ -49,18 +50,23 @@ func (rc *requestConfig) initDefaultConfig(reqConfig *define.Request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if nil == reqConfig.Header {
|
if nil == reqConfig.Header {
|
||||||
reqConfig.Header = map[string]string{}
|
reqConfig.Header = map[string]any{}
|
||||||
}
|
}
|
||||||
if len(reqConfig.ContentType) > 0 {
|
if len(reqConfig.ContentType) > 0 {
|
||||||
reqConfig.Header[consts.HeaderKeyContentType.String()] = reqConfig.ContentType
|
reqConfig.Header[consts.HeaderKeyContentType.String()] = reqConfig.ContentType
|
||||||
} else {
|
} else {
|
||||||
if len(reqConfig.Header[consts.HeaderKeyContentType.String()]) == 0 {
|
if val, exist := reqConfig.Header[consts.HeaderKeyContentType.String()]; exist {
|
||||||
|
if val == nil || fmt.Sprintf("%v", val) == "" {
|
||||||
|
// 没配置Content-Type, 默认JSON
|
||||||
|
reqConfig.Header[consts.HeaderKeyContentType.String()] = consts.MimeTypeJson
|
||||||
|
}
|
||||||
|
} else {
|
||||||
// 没配置Content-Type, 默认JSON
|
// 没配置Content-Type, 默认JSON
|
||||||
reqConfig.Header[consts.HeaderKeyContentType.String()] = consts.MimeTypeJson
|
reqConfig.Header[consts.HeaderKeyContentType.String()] = consts.MimeTypeJson
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if nil == reqConfig.Cookie {
|
if nil == reqConfig.Cookie {
|
||||||
reqConfig.Cookie = map[string]string{}
|
reqConfig.Cookie = map[string]any{}
|
||||||
}
|
}
|
||||||
if reqConfig.ConnectTimeout <= 0 {
|
if reqConfig.ConnectTimeout <= 0 {
|
||||||
reqConfig.ConnectTimeout = define.DefaultConnectTimeout
|
reqConfig.ConnectTimeout = define.DefaultConnectTimeout
|
||||||
|
Loading…
x
Reference in New Issue
Block a user