针对表单上传的优化
This commit is contained in:
@@ -18,32 +18,7 @@ import (
|
|||||||
* Date: 2023年7月12日11:35:01
|
* Date: 2023年7月12日11:35:01
|
||||||
*/
|
*/
|
||||||
|
|
||||||
type dataType string
|
|
||||||
|
|
||||||
const (
|
|
||||||
DataTypeForm dataType = "form"
|
|
||||||
DataTypeJson dataType = "json"
|
|
||||||
DataTypeXml dataType = "xml"
|
|
||||||
DataTypeEncode dataType = "encode"
|
|
||||||
DataTypeText dataType = "text"
|
|
||||||
DataTypeUrlEncoded dataType = "urlencode"
|
|
||||||
)
|
|
||||||
|
|
||||||
type method string
|
|
||||||
|
|
||||||
const (
|
|
||||||
MethodGet method = "GET"
|
|
||||||
MethodPost method = "POST"
|
|
||||||
)
|
|
||||||
|
|
||||||
type CurlParams struct {
|
|
||||||
Url string
|
|
||||||
Method method // GET/POST
|
|
||||||
Params interface{}
|
|
||||||
Headers map[string]interface{}
|
|
||||||
Cookies interface{}
|
|
||||||
DataType dataType // FORM,JSON,XML
|
|
||||||
}
|
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// 默认的transport
|
// 默认的transport
|
||||||
|
|||||||
+8
-8
@@ -8,7 +8,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestCurlx(t *testing.T) {
|
func TestGet(t *testing.T) {
|
||||||
p := CurlParams{}
|
p := CurlParams{}
|
||||||
p.Url = "http://www.baidu.com"
|
p.Url = "http://www.baidu.com"
|
||||||
p.Method = "GET"
|
p.Method = "GET"
|
||||||
@@ -18,7 +18,7 @@ func TestCurlx(t *testing.T) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSendFile(t *testing.T) {
|
func TestForm(t *testing.T) {
|
||||||
|
|
||||||
file, err := os.Open("./go.mod")
|
file, err := os.Open("./go.mod")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -28,12 +28,12 @@ func TestSendFile(t *testing.T) {
|
|||||||
|
|
||||||
b, _ := io.ReadAll(file)
|
b, _ := io.ReadAll(file)
|
||||||
|
|
||||||
s := []*FormParam{
|
s := []FormParam{
|
||||||
{
|
{
|
||||||
Key: "file",
|
FieldName: "file",
|
||||||
Name: file.Name(),
|
FileName: file.Name(),
|
||||||
Action: "file",
|
FieldType: "file",
|
||||||
Value: string(b),
|
FileBytes: b,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -44,7 +44,7 @@ func TestSendFile(t *testing.T) {
|
|||||||
Headers: map[string]interface{}{
|
Headers: map[string]interface{}{
|
||||||
"Authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0ZW5hbnRfaWQiOjAsImNsaWVudF9pZCI6MCwidXNlcl9pZCI6MSwiZXhwIjoxNzAxMzk3NzkxfQ.9_uJ6y8I4JZTwgSenwHC_01nddLuI4zmgpyPhn5M6j8",
|
"Authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0ZW5hbnRfaWQiOjAsImNsaWVudF9pZCI6MCwidXNlcl9pZCI6MSwiZXhwIjoxNzAxMzk3NzkxfQ.9_uJ6y8I4JZTwgSenwHC_01nddLuI4zmgpyPhn5M6j8",
|
||||||
},
|
},
|
||||||
DataType: DataTypeForm,
|
ContentType: ContentTypeForm,
|
||||||
}
|
}
|
||||||
resp, code, err := NewCurlx().Send(context.Background(), p)
|
resp, code, err := NewCurlx().Send(context.Background(), p)
|
||||||
fmt.Println(resp, code, err)
|
fmt.Println(resp, code, err)
|
||||||
|
|||||||
+30
-31
@@ -13,13 +13,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type FormParam struct {
|
|
||||||
Key string `json:"key"` // 字段名
|
|
||||||
Action string `json:"action"` // 动作(file/text)
|
|
||||||
Name string `json:"name"` // 文件名
|
|
||||||
Bytes []byte `json:"bytes"` // 文件内容
|
|
||||||
Value string `json:"value"` // 值
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 处理请求类型
|
* 处理请求类型
|
||||||
@@ -76,10 +70,10 @@ func (p *CurlParams) parseParams() (str io.Reader, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if p.Params != nil {
|
if p.Params != nil {
|
||||||
if p.DataType == DataTypeJson {
|
if p.ContentType == ContentTypeJson {
|
||||||
// 判断是否存在
|
// 判断是否存在
|
||||||
if _, ok := p.Headers["Content-Type"]; !ok {
|
if _, ok := p.Headers["Content-Type"]; !ok {
|
||||||
p.Headers["Content-Type"] = "application/json"
|
p.Headers["Content-Type"] = ContentTypeJson
|
||||||
}
|
}
|
||||||
strParam, ok := p.Params.(string)
|
strParam, ok := p.Params.(string)
|
||||||
if ok {
|
if ok {
|
||||||
@@ -89,30 +83,35 @@ func (p *CurlParams) parseParams() (str io.Reader, err error) {
|
|||||||
if err == nil {
|
if err == nil {
|
||||||
return bytes.NewReader(b), nil
|
return bytes.NewReader(b), nil
|
||||||
}
|
}
|
||||||
} else if p.DataType == DataTypeForm {
|
} else if p.ContentType == ContentTypeForm {
|
||||||
// 表单上传(可能有文件)
|
// 表单上传(可能有文件)
|
||||||
// 文件上传的
|
// 文件上传的
|
||||||
formParam, ok := p.Params.([]*FormParam)
|
params := []FormParam{}
|
||||||
if ok {
|
if value, ok := p.Params.([]FormParam); ok {
|
||||||
body := &bytes.Buffer{}
|
params = value
|
||||||
writer := multipart.NewWriter(body)
|
} else if value, ok := p.Params.(FormParam); ok {
|
||||||
for _, v := range formParam {
|
params = append(params, value)
|
||||||
if v.Action == "file" {
|
} else {
|
||||||
part, _ := writer.CreateFormFile(v.Key, v.Name)
|
return nil, errors.New("表单上传的参数格式不正确")
|
||||||
io.Copy(part, strings.NewReader(v.Value))
|
|
||||||
} else {
|
|
||||||
_ = writer.WriteField(v.Key, v.Value)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
writer.Close()
|
|
||||||
p.Headers["Content-Type"] = writer.FormDataContentType()
|
|
||||||
return body, nil
|
|
||||||
}
|
}
|
||||||
return nil, errors.New("表单上传的参数格式不正确")
|
|
||||||
|
|
||||||
} else if p.DataType == DataTypeXml {
|
body := &bytes.Buffer{}
|
||||||
|
writer := multipart.NewWriter(body)
|
||||||
|
for _, v := range params {
|
||||||
|
if v.FieldType == FieldTypeFile {
|
||||||
|
part, _ := writer.CreateFormFile(v.FieldName, v.FileName)
|
||||||
|
io.Copy(part, bytes.NewBuffer(v.FileBytes))
|
||||||
|
} else {
|
||||||
|
_ = writer.WriteField(v.FieldName, v.FieldValue)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
writer.Close()
|
||||||
|
p.Headers["Content-Type"] = writer.FormDataContentType()
|
||||||
|
return body, nil
|
||||||
|
|
||||||
|
} else if p.ContentType == ContentTypeXml {
|
||||||
if _, ok := p.Headers["Content-Type"]; !ok {
|
if _, ok := p.Headers["Content-Type"]; !ok {
|
||||||
p.Headers["Content-Type"] = "application/xml"
|
p.Headers["Content-Type"] = ContentTypeXml
|
||||||
}
|
}
|
||||||
var string_data string
|
var string_data string
|
||||||
if value, ok := p.Params.(string); ok {
|
if value, ok := p.Params.(string); ok {
|
||||||
@@ -126,9 +125,9 @@ func (p *CurlParams) parseParams() (str io.Reader, err error) {
|
|||||||
string_data = string(by)
|
string_data = string(by)
|
||||||
}
|
}
|
||||||
return strings.NewReader(string_data), nil
|
return strings.NewReader(string_data), nil
|
||||||
} else if p.DataType == DataTypeText {
|
} else if p.ContentType == ContentTypeText {
|
||||||
if _, ok := p.Headers["Content-Type"]; !ok {
|
if _, ok := p.Headers["Content-Type"]; !ok {
|
||||||
p.Headers["Content-Type"] = "text/plain"
|
p.Headers["Content-Type"] = ContentTypeText
|
||||||
}
|
}
|
||||||
|
|
||||||
var string_data string
|
var string_data string
|
||||||
@@ -140,7 +139,7 @@ func (p *CurlParams) parseParams() (str io.Reader, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return strings.NewReader(string_data), nil
|
return strings.NewReader(string_data), nil
|
||||||
} else if p.DataType == DataTypeUrlEncoded {
|
} else if p.ContentType == ContentTypeUrlEncoded {
|
||||||
if _, ok := p.Headers["Content-Type"]; !ok {
|
if _, ok := p.Headers["Content-Type"]; !ok {
|
||||||
p.Headers["Content-Type"] = "application/x-www-form-urlencoded"
|
p.Headers["Content-Type"] = "application/x-www-form-urlencoded"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,42 @@
|
|||||||
|
package curlx
|
||||||
|
|
||||||
|
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"
|
||||||
|
)
|
||||||
|
|
||||||
|
type CurlParams struct {
|
||||||
|
Url string
|
||||||
|
Method method // GET/POST
|
||||||
|
Params interface{}
|
||||||
|
Headers map[string]interface{}
|
||||||
|
Cookies interface{}
|
||||||
|
ContentType ContentType // FORM,JSON,XML
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user