设置请求header与请求url

This commit is contained in:
白茶清欢 2024-05-31 18:37:35 +08:00
parent a0adefcc0c
commit 0ba0f7dced
2 changed files with 8 additions and 2 deletions

View File

@ -89,5 +89,8 @@ func (hc *HttpClient) GetRestyClient() *resty.Client {
// //
// Date : 15:52 2024/5/31 // Date : 15:52 2024/5/31
func (hc *HttpClient) Request(reqConfig *define.Request) *define.Response { func (hc *HttpClient) Request(reqConfig *define.Request) *define.Response {
hc.Client.OnBeforeRequest(hc.getRequestValidateMiddleware()) // 请求参数验证中间件必注册
hc.Client.OnAfterResponse(hc.getResponseValidateMiddleware()) // 响应验证中间件必注册
hc.request.Send()
return nil return nil
} }

View File

@ -38,6 +38,8 @@ func NewRestyClient(reqConfig *define.Request) (*resty.Client, *resty.Request) {
request.SetPathParams(reqConfig.PathParam) // 设置path中的参数 request.SetPathParams(reqConfig.PathParam) // 设置path中的参数
request.SetQueryParams(reqConfig.Query) // 设置query参数 request.SetQueryParams(reqConfig.Query) // 设置query参数
request.SetHeaders(reqConfig.Header) // 设置header request.SetHeaders(reqConfig.Header) // 设置header
request.URL = reqConfig.FullUrl // 请求接口
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 {
cookieList = append(cookieList, &http.Cookie{ cookieList = append(cookieList, &http.Cookie{
@ -45,8 +47,8 @@ func NewRestyClient(reqConfig *define.Request) (*resty.Client, *resty.Request) {
Value: cookieValue, Value: cookieValue,
}) })
} }
request.SetCookies(cookieList) // 设置cookie request.SetCookies(cookieList) // 设置cookie
setRestyBody(reqConfig, request) setRestyBody(reqConfig, request) // 设置请求Body
return client, request return client, request
} }
@ -71,6 +73,7 @@ func setRestyBody(reqConfig *define.Request, request *resty.Request) {
bodyData[key.String()] = value.String() bodyData[key.String()] = value.String()
return true return true
}) })
request.SetFormData(bodyData)
} }
return return
} }