1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-13 09:12:28 +08:00

fix: sending post request with header multipart/form-data support query string type is map[string]string

This commit is contained in:
dudaodong
2022-07-07 10:23:15 +08:00
parent 3625921912
commit 3dbd7d8980
3 changed files with 24 additions and 13 deletions

View File

@@ -6,8 +6,8 @@
// HttpGet, HttpPost, HttpDelete, HttpPut, HttpPatch, function param `url` is required. // HttpGet, HttpPost, HttpDelete, HttpPut, HttpPatch, function param `url` is required.
// HttpGet, HttpPost, HttpDelete, HttpPut, HttpPatch, function param `params` is variable, the order is: // 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[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 // 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[1] should be url.Values // multipart/form-data or application/x-www-form-urlencoded
// params[2] is post body which type should be []byte. // params[2] is post body which type should be []byte.
// params[3] is http client which type should be http.Client. // params[3] is http client which type should be http.Client.
package netutil package netutil

View File

@@ -4,7 +4,6 @@ import (
"encoding/json" "encoding/json"
"io/ioutil" "io/ioutil"
"log" "log"
"net/url"
"testing" "testing"
"github.com/duke-git/lancet/v2/internal" "github.com/duke-git/lancet/v2/internal"
@@ -57,9 +56,13 @@ func TestHttpPostFormData(t *testing.T) {
UserId int `json:"userId"` UserId int `json:"userId"`
Title string `json:"title"` Title string `json:"title"`
} }
postData := url.Values{} // postData := url.Values{}
postData.Add("userId", "1") // postData.Add("userId", "1")
postData.Add("title", "TestAddToDo") // postData.Add("title", "TestAddToDo")
postData := make(map[string]string)
postData["userId"] = "1"
postData["title"] = "title"
resp, err := HttpPost(apiUrl, header, postData, nil) resp, err := HttpPost(apiUrl, header, postData, nil)
if err != nil { if err != nil {

View File

@@ -3,7 +3,6 @@ package netutil
import ( import (
"bytes" "bytes"
"errors" "errors"
"fmt"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"net/url" "net/url"
@@ -81,9 +80,18 @@ func setHeaderAndQueryAndBody(req *http.Request, reqUrl string, header, queryPar
if err != nil { if err != nil {
return err return err
} }
if req.Header.Get("Content-Type") == "multipart/form-data" || req.Header.Get("Content-Type") == "application/x-www-form-urlencoded" { if strings.Contains(req.Header.Get("Content-Type"), "multipart/form-data") || req.Header.Get("Content-Type") == "application/x-www-form-urlencoded" {
formData := queryParam.(url.Values) if formData, ok := queryParam.(url.Values); ok {
err = setBodyByte(req, []byte(formData.Encode())) 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 { } else {
err = setBodyByte(req, body) err = setBodyByte(req, body)
} }
@@ -132,15 +140,15 @@ func setQueryParam(req *http.Request, reqUrl string, queryParam any) error {
var values url.Values var values url.Values
if queryParam != nil { if queryParam != nil {
switch v := queryParam.(type) { switch v := queryParam.(type) {
case map[string]any: case map[string]string:
values = url.Values{} values = url.Values{}
for k := range v { for k := range v {
values.Set(k, fmt.Sprintf("%v", v[k])) values.Set(k, v[k])
} }
case url.Values: case url.Values:
values = v values = v
default: 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")
} }
} }