2024-01-01 14:10:18 +08:00
|
|
|
package curlx
|
|
|
|
|
|
|
|
|
|
type ClientParams struct {
|
|
|
|
|
Url string
|
|
|
|
|
Method Method // GET/POST
|
2024-01-12 21:06:13 +08:00
|
|
|
Body interface{}
|
2024-01-01 14:10:18 +08:00
|
|
|
Headers map[string]interface{}
|
|
|
|
|
Cookies interface{}
|
|
|
|
|
ContentType ContentType // FORM,JSON,XML
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func defaultParams() ClientParams {
|
2024-03-14 18:30:06 +08:00
|
|
|
return ClientParams{
|
|
|
|
|
Headers: map[string]interface{}{}, // 初始化map
|
|
|
|
|
}
|
2024-01-01 14:10:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Param func(*ClientParams)
|
|
|
|
|
|
2024-01-12 21:06:13 +08:00
|
|
|
func SetParamsAll(cp ClientParams) Param {
|
2024-01-01 14:10:18 +08:00
|
|
|
return func(param *ClientParams) {
|
|
|
|
|
param.Url = cp.Url
|
|
|
|
|
param.Method = cp.Method
|
2024-01-12 21:06:13 +08:00
|
|
|
param.Body = cp.Body
|
2024-01-01 14:10:18 +08:00
|
|
|
param.Headers = cp.Headers
|
|
|
|
|
param.Cookies = cp.Cookies
|
|
|
|
|
param.ContentType = cp.ContentType
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 设置URL
|
|
|
|
|
*/
|
2024-01-12 21:06:13 +08:00
|
|
|
func SetParamsUrl(url string) Param {
|
2024-01-01 14:10:18 +08:00
|
|
|
return func(param *ClientParams) {
|
|
|
|
|
param.Url = url
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 设置方法
|
|
|
|
|
*/
|
2024-01-12 21:06:13 +08:00
|
|
|
func SetParamsMethod(m Method) Param {
|
2024-01-01 14:10:18 +08:00
|
|
|
return func(param *ClientParams) {
|
|
|
|
|
param.Method = m
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 设置参数
|
|
|
|
|
*/
|
2024-01-12 21:06:13 +08:00
|
|
|
func SetParamsBody(p interface{}) Param {
|
2024-01-01 14:10:18 +08:00
|
|
|
return func(param *ClientParams) {
|
2024-01-12 21:06:13 +08:00
|
|
|
param.Body = p
|
2024-01-01 14:10:18 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 表单文本参数
|
|
|
|
|
*/
|
|
|
|
|
func SetParamsFormText(fieldName, fieldValue string) Param {
|
|
|
|
|
return func(param *ClientParams) {
|
2024-01-12 21:06:13 +08:00
|
|
|
fp := param.Body.([]FormParam)
|
2024-01-01 14:10:18 +08:00
|
|
|
fp = append(fp, FormParam{
|
|
|
|
|
FieldName: fieldName,
|
|
|
|
|
FieldValue: fieldValue,
|
|
|
|
|
FieldType: FieldTypeText,
|
|
|
|
|
})
|
2024-01-12 21:06:13 +08:00
|
|
|
param.Body = fp
|
2024-01-01 14:10:18 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 表单文件上传
|
|
|
|
|
*/
|
|
|
|
|
func SetParamsFormFile(fieldName, fileName string, fileBytes []byte) Param {
|
|
|
|
|
return func(param *ClientParams) {
|
2024-01-12 21:06:13 +08:00
|
|
|
fp := param.Body.([]FormParam)
|
2024-01-01 14:10:18 +08:00
|
|
|
fp = append(fp, FormParam{
|
|
|
|
|
FieldName: fieldName,
|
|
|
|
|
FieldType: FieldTypeFile,
|
|
|
|
|
FileName: fileName,
|
|
|
|
|
FileBytes: fileBytes,
|
|
|
|
|
})
|
2024-01-12 21:06:13 +08:00
|
|
|
param.Body = fp
|
2024-01-01 14:10:18 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 设置请求头
|
|
|
|
|
*/
|
2024-01-12 21:06:13 +08:00
|
|
|
func SetParamsHeaders(h map[string]interface{}) Param {
|
2024-01-01 14:10:18 +08:00
|
|
|
return func(param *ClientParams) {
|
2024-01-24 23:27:20 +08:00
|
|
|
for key, _ := range h {
|
|
|
|
|
param.Headers[key] = h[key]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-21 11:08:13 +08:00
|
|
|
/**
|
|
|
|
|
* 设置请求头
|
|
|
|
|
*/
|
|
|
|
|
func SetParamsHeader(key, value string) Param {
|
|
|
|
|
return func(param *ClientParams) {
|
|
|
|
|
param.Headers[key] = value
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-24 23:27:20 +08:00
|
|
|
/**
|
|
|
|
|
* 设置UserAgent
|
|
|
|
|
*/
|
|
|
|
|
func SetUserAgent(userAgent UserAgent) Param {
|
|
|
|
|
return func(param *ClientParams) {
|
|
|
|
|
param.Headers["User-Agent"] = string(userAgent)
|
2024-01-01 14:10:18 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-23 13:11:15 +08:00
|
|
|
/**
|
|
|
|
|
* 设置Referer
|
|
|
|
|
*/
|
|
|
|
|
func SetReferer(referer string) Param {
|
|
|
|
|
return func(param *ClientParams) {
|
|
|
|
|
param.Headers["Referer"] = referer
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-01 14:10:18 +08:00
|
|
|
/**
|
|
|
|
|
* 设置cookies
|
|
|
|
|
*/
|
2024-01-12 21:06:13 +08:00
|
|
|
func SetParamsCookies(c interface{}) Param {
|
2024-01-01 14:10:18 +08:00
|
|
|
return func(param *ClientParams) {
|
|
|
|
|
param.Cookies = c
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 设置请求方法
|
|
|
|
|
*/
|
2024-01-12 21:06:13 +08:00
|
|
|
func SetParamsContentType(t ContentType) Param {
|
2024-01-01 14:10:18 +08:00
|
|
|
return func(param *ClientParams) {
|
|
|
|
|
param.ContentType = t
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type FieldType string
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
FieldTypeFile FieldType = "file"
|
|
|
|
|
FieldTypeText FieldType = "text"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type FormParam struct {
|
|
|
|
|
FieldName string `json:"field_name"` // 字段名
|
|
|
|
|
FieldValue string `json:"field_value"` // 字段值
|
|
|
|
|
FieldType FieldType `json:"field_type"` // 动作(file/text)
|
|
|
|
|
FileName string `json:"file_name"` // 文件名
|
|
|
|
|
FileBytes []byte `json:"file_bytes"` // 文件内容
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type ContentType string
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
ContentTypeForm ContentType = "multipart/form-data"
|
|
|
|
|
ContentTypeJson ContentType = "application/json"
|
|
|
|
|
ContentTypeXml ContentType = "application/xml"
|
|
|
|
|
ContentTypeText ContentType = "text/plain"
|
|
|
|
|
ContentTypeUrlEncoded ContentType = "application/x-www-form-urlencoded"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Method string
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
MethodGet Method = "GET"
|
|
|
|
|
MethodPost Method = "POST"
|
|
|
|
|
)
|