支持自适应请求类型, 根据不同请求类型设置正确Body格式
This commit is contained in:
33
httpclient/implement/request/write_form.go
Normal file
33
httpclient/implement/request/write_form.go
Normal file
@ -0,0 +1,33 @@
|
||||
// Package request ...
|
||||
//
|
||||
// Description : request ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2025-05-08 14:39
|
||||
package request
|
||||
|
||||
import (
|
||||
"git.zhangdeman.cn/zhangdeman/serialize"
|
||||
"github.com/tidwall/gjson"
|
||||
"resty.dev/v3"
|
||||
)
|
||||
|
||||
// WriteForm application/x-www-form-urlencoded 实现
|
||||
type WriteForm struct {
|
||||
}
|
||||
|
||||
func (w *WriteForm) Write(request *resty.Request, bodyData map[string]any) error {
|
||||
if len(bodyData) == 0 {
|
||||
return nil
|
||||
}
|
||||
bodyStr := serialize.JSON.MarshalForStringIgnoreError(bodyData)
|
||||
formatBodyData := map[string]string{}
|
||||
jsonObj := gjson.Parse(bodyStr)
|
||||
jsonObj.ForEach(func(key, value gjson.Result) bool {
|
||||
formatBodyData[key.String()] = value.String()
|
||||
return true
|
||||
})
|
||||
request.SetFormData(formatBodyData)
|
||||
return nil
|
||||
}
|
Reference in New Issue
Block a user