1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-04 12:52: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 `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

View File

@@ -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 {

View File

@@ -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")
}
}