diff --git a/curlx.go b/curlx.go index b691037..4f6f9eb 100644 --- a/curlx.go +++ b/curlx.go @@ -18,32 +18,7 @@ import ( * 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 ( // 默认的transport diff --git a/curlx_test.go b/curlx_test.go index 310d361..d3e6e77 100644 --- a/curlx_test.go +++ b/curlx_test.go @@ -8,7 +8,7 @@ import ( "testing" ) -func TestCurlx(t *testing.T) { +func TestGet(t *testing.T) { p := CurlParams{} p.Url = "http://www.baidu.com" 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") if err != nil { @@ -28,12 +28,12 @@ func TestSendFile(t *testing.T) { b, _ := io.ReadAll(file) - s := []*FormParam{ + s := []FormParam{ { - Key: "file", - Name: file.Name(), - Action: "file", - Value: string(b), + FieldName: "file", + FileName: file.Name(), + FieldType: "file", + FileBytes: b, }, } @@ -44,7 +44,7 @@ func TestSendFile(t *testing.T) { Headers: map[string]interface{}{ "Authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0ZW5hbnRfaWQiOjAsImNsaWVudF9pZCI6MCwidXNlcl9pZCI6MSwiZXhwIjoxNzAxMzk3NzkxfQ.9_uJ6y8I4JZTwgSenwHC_01nddLuI4zmgpyPhn5M6j8", }, - DataType: DataTypeForm, + ContentType: ContentTypeForm, } resp, code, err := NewCurlx().Send(context.Background(), p) fmt.Println(resp, code, err) diff --git a/request.go b/request.go index 7e679cb..07a77fa 100644 --- a/request.go +++ b/request.go @@ -13,13 +13,7 @@ import ( "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.DataType == DataTypeJson { + if p.ContentType == ContentTypeJson { // 判断是否存在 if _, ok := p.Headers["Content-Type"]; !ok { - p.Headers["Content-Type"] = "application/json" + p.Headers["Content-Type"] = ContentTypeJson } strParam, ok := p.Params.(string) if ok { @@ -89,30 +83,35 @@ func (p *CurlParams) parseParams() (str io.Reader, err error) { if err == nil { return bytes.NewReader(b), nil } - } else if p.DataType == DataTypeForm { + } else if p.ContentType == ContentTypeForm { // 表单上传(可能有文件) // 文件上传的 - formParam, ok := p.Params.([]*FormParam) - if ok { - body := &bytes.Buffer{} - writer := multipart.NewWriter(body) - for _, v := range formParam { - if v.Action == "file" { - part, _ := writer.CreateFormFile(v.Key, v.Name) - 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 + params := []FormParam{} + if value, ok := p.Params.([]FormParam); ok { + params = value + } else if value, ok := p.Params.(FormParam); ok { + params = append(params, value) + } else { + return nil, errors.New("表单上传的参数格式不正确") } - 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 { - p.Headers["Content-Type"] = "application/xml" + p.Headers["Content-Type"] = ContentTypeXml } var string_data string if value, ok := p.Params.(string); ok { @@ -126,9 +125,9 @@ func (p *CurlParams) parseParams() (str io.Reader, err error) { string_data = string(by) } return strings.NewReader(string_data), nil - } else if p.DataType == DataTypeText { + } else if p.ContentType == ContentTypeText { if _, ok := p.Headers["Content-Type"]; !ok { - p.Headers["Content-Type"] = "text/plain" + p.Headers["Content-Type"] = ContentTypeText } var string_data string @@ -140,7 +139,7 @@ func (p *CurlParams) parseParams() (str io.Reader, err error) { } return strings.NewReader(string_data), nil - } else if p.DataType == DataTypeUrlEncoded { + } else if p.ContentType == ContentTypeUrlEncoded { if _, ok := p.Headers["Content-Type"]; !ok { p.Headers["Content-Type"] = "application/x-www-form-urlencoded" } diff --git a/types.go b/types.go new file mode 100644 index 0000000..b67168e --- /dev/null +++ b/types.go @@ -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 +}