1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-04 21:02:27 +08:00

fix: fix http post fucntion

This commit is contained in:
dudaodong
2022-04-13 11:38:45 +08:00
parent 0e3dc68de5
commit c7aa44b8a4
2 changed files with 9 additions and 2 deletions

View File

@@ -6,7 +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]interface{},
// params[1] is query param which type should be url.Values or map[string]interface{}, when content-type header is
// multipart/form-data or application/x-www-form-urlencoded must pass url.Values params
// 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

@@ -41,6 +41,7 @@ func doHttpRequest(method, reqUrl string, params ...interface{}) (*http.Response
return nil, err
}
case 3:
err := setHeaderAndQueryAndBody(req, reqUrl, params[0], params[1], params[2])
if err != nil {
return nil, err
@@ -81,7 +82,12 @@ func setHeaderAndQueryAndBody(req *http.Request, reqUrl string, header, queryPar
if err != nil {
return err
}
err = setBodyByte(req, body)
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, formData.Encode())
} else {
err = setBodyByte(req, body)
}
if err != nil {
return err
}