From 3dbd7d89809c0462139412b762374edbfb2da3ec Mon Sep 17 00:00:00 2001 From: dudaodong Date: Thu, 7 Jul 2022 10:23:15 +0800 Subject: [PATCH] fix: sending post request with header multipart/form-data support query string type is map[string]string --- netutil/http.go | 4 ++-- netutil/http_test.go | 11 +++++++---- netutil/net_internal.go | 22 +++++++++++++++------- 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/netutil/http.go b/netutil/http.go index 30070d0..a6484cc 100644 --- a/netutil/http.go +++ b/netutil/http.go @@ -6,8 +6,8 @@ // HttpGet, HttpPost, HttpDelete, HttpPut, HttpPatch, function param `url` is required. // HttpGet, HttpPost, HttpDelete, HttpPut, HttpPatch, function param `params` is variable, the order is: // params[0] is header which type should be http.Header or map[string]string, -// params[1] is query param which type should be url.Values or map[string]any, when content-type header is -// multipart/form-data or application/x-www-form-urlencoded, params[1] should be url.Values +// params[1] is query string param which type should be url.Values or map[string]string, when content-type header is +// multipart/form-data or application/x-www-form-urlencoded // params[2] is post body which type should be []byte. // params[3] is http client which type should be http.Client. package netutil diff --git a/netutil/http_test.go b/netutil/http_test.go index 9001ab1..5b44263 100644 --- a/netutil/http_test.go +++ b/netutil/http_test.go @@ -4,7 +4,6 @@ import ( "encoding/json" "io/ioutil" "log" - "net/url" "testing" "github.com/duke-git/lancet/v2/internal" @@ -57,9 +56,13 @@ func TestHttpPostFormData(t *testing.T) { UserId int `json:"userId"` Title string `json:"title"` } - postData := url.Values{} - postData.Add("userId", "1") - postData.Add("title", "TestAddToDo") + // postData := url.Values{} + // postData.Add("userId", "1") + // postData.Add("title", "TestAddToDo") + + postData := make(map[string]string) + postData["userId"] = "1" + postData["title"] = "title" resp, err := HttpPost(apiUrl, header, postData, nil) if err != nil { diff --git a/netutil/net_internal.go b/netutil/net_internal.go index a9705da..cecba1f 100644 --- a/netutil/net_internal.go +++ b/netutil/net_internal.go @@ -3,7 +3,6 @@ package netutil import ( "bytes" "errors" - "fmt" "io/ioutil" "net/http" "net/url" @@ -81,9 +80,18 @@ func setHeaderAndQueryAndBody(req *http.Request, reqUrl string, header, queryPar if err != nil { return err } - if req.Header.Get("Content-Type") == "multipart/form-data" || req.Header.Get("Content-Type") == "application/x-www-form-urlencoded" { - formData := queryParam.(url.Values) - err = setBodyByte(req, []byte(formData.Encode())) + if strings.Contains(req.Header.Get("Content-Type"), "multipart/form-data") || req.Header.Get("Content-Type") == "application/x-www-form-urlencoded" { + if formData, ok := queryParam.(url.Values); ok { + err = setBodyByte(req, []byte(formData.Encode())) + } + if formData, ok := queryParam.(map[string]string); ok { + postData := url.Values{} + for k, v := range formData { + postData.Set(k, v) + } + err = setBodyByte(req, []byte(postData.Encode())) + } + } else { err = setBodyByte(req, body) } @@ -132,15 +140,15 @@ func setQueryParam(req *http.Request, reqUrl string, queryParam any) error { var values url.Values if queryParam != nil { switch v := queryParam.(type) { - case map[string]any: + case map[string]string: values = url.Values{} for k := range v { - values.Set(k, fmt.Sprintf("%v", v[k])) + values.Set(k, v[k]) } case url.Values: values = v default: - return errors.New("query params type should be url.Values or map[string]any") + return errors.New("query string params type should be url.Values or map[string]string") } }