From 9ea74e0a8a0fb18e3f2d5024d668afa9506b6728 Mon Sep 17 00:00:00 2001 From: dorlolo <428192774@qq.com> Date: Thu, 24 Mar 2022 22:51:14 +0800 Subject: [PATCH] =?UTF-8?q?=E8=87=AA=E5=8A=A8=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- simpleRequest.go | 28 ++++++++++++++++++++++++++++ test/data_test.go | 23 +++++++++++++++++++++++ test/simpleRequest_test.go | 17 +++++++++++++++++ 3 files changed, 68 insertions(+) create mode 100644 test/data_test.go diff --git a/simpleRequest.go b/simpleRequest.go index 836da6b..06f00f5 100644 --- a/simpleRequest.go +++ b/simpleRequest.go @@ -14,6 +14,7 @@ import ( "fmt" "io" "io/ioutil" + "mime/multipart" "net/http" "net/url" "strings" @@ -224,6 +225,33 @@ func (s *SimpleRequest) initBody() { } else { s.body = bytes.NewReader([]byte("{}")) } + case contentTypeData == formDataType: + body := &bytes.Buffer{} + writer := multipart.NewWriter(body) + //data := url.Values{} + for k, sv := range s.tempBody { + switch sv.(type) { + case string: + strSv, _ := sv.(string) + _ = writer.WriteField(k, strSv) + //data.Set(k, strSv) + case []string: + sss, _ := sv.([]string) + for _, v := range sss { + _ = writer.WriteField(k, v) + //data.Add(k, v) + } + } + } + err := writer.Close() + if err != nil { + panic(err) + } + s.headers.Set("Content-Type", writer.FormDataContentType()) + //strD := data.Encode() + //bodyText := strings.NewReader(strD) + s.body = body + case contentTypeData == xmlDataType || contentTypeData == textPlainType || contentTypeData == javaScriptType: data, _ := s.tempBody[stringBodyType].(string) s.body = strings.NewReader(data) diff --git a/test/data_test.go b/test/data_test.go new file mode 100644 index 0000000..1695dd7 --- /dev/null +++ b/test/data_test.go @@ -0,0 +1,23 @@ +/* + * @FileName: dataTest.go + * @Author: JuneXu + * @CreateTime: 2022/3/24 下午9:18 + * @Description: + */ + +package test + +import ( + "encoding/json" + "testing" +) + +func TestData(t *testing.T) { + type dt map[string][]string + var newData = dt{ + "aa": []string{"aa", "cc"}, + } + res, err := json.Marshal(&newData) + t.Log(err) + t.Log(res) +} diff --git a/test/simpleRequest_test.go b/test/simpleRequest_test.go index dba4ccc..32ce881 100644 --- a/test/simpleRequest_test.go +++ b/test/simpleRequest_test.go @@ -48,3 +48,20 @@ func TestRequest(t *testing.T) { } } + +func TestAuth(t *testing.T) { + req := simpleRequest.NewRequest() + req.Headers().ConentType_formData() + req.Headers().SetRandomUerAgent() + req.Body().Set("grant_type", "password") + req.Body().Set("client_id", "smz") + req.Body().Set("client_secret", "smz") + req.Body().Set("scope", "getdata") + req.Body().Set("username", "shiming_zyf") + req.Body().Set("password", "zyf499bbcb9") + + var URL = "https://shiming.cn-jzs.com/oauth/token" + + data, _ := req.Post(URL) + t.Log(string(data)) +}