mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-04 12:52:28 +08:00
fix: fix body param bug when send post request
This commit is contained in:
@@ -620,9 +620,9 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### <span id="HttpGet">HttpGet (Deprecated: use SendRequest for replacement)</span>
|
### <span id="HttpGet">HttpGet</span>
|
||||||
|
|
||||||
<p>Send http get request.</p>
|
<p>Send http get request. (Deprecated: use SendRequest for replacement)</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
@@ -662,9 +662,9 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### <span id="HttpPost">HttpPost (Deprecated: use SendRequest for replacement)</span>
|
### <span id="HttpPost">HttpPost</span>
|
||||||
|
|
||||||
<p>Send http post request.</p>
|
<p>Send http post request.(Deprecated: use SendRequest for replacement)</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
@@ -692,28 +692,26 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
url := "https://jsonplaceholder.typicode.com/todos"
|
url := "https://jsonplaceholder.typicode.com/todos"
|
||||||
header := map[string]string{
|
header := map[string]string{
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/x-www-form-urlencoded",
|
||||||
}
|
}
|
||||||
type Todo struct {
|
|
||||||
UserId int `json:"userId"`
|
postData := url.Values{}
|
||||||
Title string `json:"title"`
|
postData.Add("userId", "1")
|
||||||
}
|
postData.Add("title", "TestToDo")
|
||||||
todo := Todo{1, "TestAddToDo"}
|
|
||||||
bodyParams, _ := json.Marshal(todo)
|
|
||||||
|
|
||||||
resp, err := netutil.HttpPost(url, header, nil, bodyParams)
|
resp, err := netutil.HttpPost(apiUrl, header, nil, postData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
body, _ := ioutil.ReadAll(resp.Body)
|
body, _ := ioutil.ReadAll(resp.Body)
|
||||||
fmt.Println(body)
|
fmt.Println(body)
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### <span id="HttpPut">HttpPut (Deprecated: use SendRequest for replacement)</span>
|
### <span id="HttpPut">HttpPut</span>
|
||||||
|
|
||||||
<p>Send http put request.</p>
|
<p>Send http put request. (Deprecated: use SendRequest for replacement)</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
@@ -761,9 +759,9 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### <span id="HttpDelete">HttpDelete (Deprecated: use SendRequest for replacement)</span>
|
### <span id="HttpDelete">HttpDelete</span>
|
||||||
|
|
||||||
<p>Send http delete request.</p>
|
<p>Send http delete request. (Deprecated: use SendRequest for replacement)</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
@@ -800,9 +798,9 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### <span id="HttpPatch">HttpPatch (Deprecated: use SendRequest for replacement)</span>
|
### <span id="HttpPatch">HttpPatch</span>
|
||||||
|
|
||||||
<p>Send http patch request.</p>
|
<p>Send http patch request. (Deprecated: use SendRequest for replacement)</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
|||||||
@@ -622,9 +622,9 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### <span id="HttpGet">HttpGet (Deprecated: use SendRequest for replacement)</span>
|
### <span id="HttpGet">HttpGet</span>
|
||||||
|
|
||||||
<p>发送http get请求</p>
|
<p>发送http get请求。(已废弃:使用SendRequest)</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
@@ -664,9 +664,9 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### <span id="HttpPost">HttpPost (Deprecated: use SendRequest for replacement)</span>
|
### <span id="HttpPost">HttpPost</span>
|
||||||
|
|
||||||
<p>发送http post请求</p>
|
<p>发送http post请求。(已废弃:使用SendRequest)</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
@@ -694,28 +694,26 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
url := "https://jsonplaceholder.typicode.com/todos"
|
url := "https://jsonplaceholder.typicode.com/todos"
|
||||||
header := map[string]string{
|
header := map[string]string{
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/x-www-form-urlencoded",
|
||||||
}
|
}
|
||||||
type Todo struct {
|
|
||||||
UserId int `json:"userId"`
|
postData := url.Values{}
|
||||||
Title string `json:"title"`
|
postData.Add("userId", "1")
|
||||||
}
|
postData.Add("title", "TestToDo")
|
||||||
todo := Todo{1, "TestAddToDo"}
|
|
||||||
bodyParams, _ := json.Marshal(todo)
|
|
||||||
|
|
||||||
resp, err := netutil.HttpPost(url, header, nil, bodyParams)
|
resp, err := netutil.HttpPost(apiUrl, header, nil, postData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
body, _ := ioutil.ReadAll(resp.Body)
|
body, _ := ioutil.ReadAll(resp.Body)
|
||||||
fmt.Println(body)
|
fmt.Println(body)
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### <span id="HttpPut">HttpPut (Deprecated: use SendRequest for replacement)</span>
|
### <span id="HttpPut">HttpPut</span>
|
||||||
|
|
||||||
<p>发送http put请求</p>
|
<p>发送http put请求。(已废弃:使用SendRequest)</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
@@ -763,9 +761,9 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### <span id="HttpDelete">HttpDelete (Deprecated: use SendRequest for replacement)</span>
|
### <span id="HttpDelete">HttpDelete</span>
|
||||||
|
|
||||||
<p>发送http delete请求</p>
|
<p>发送http delete请求。(已废弃:使用SendRequest)</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
@@ -802,9 +800,9 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### <span id="HttpPatch">HttpPatch (Deprecated: use SendRequest for replacement)</span>
|
### <span id="HttpPatch">HttpPatch</span>
|
||||||
|
|
||||||
<p>发送http patch请求</p>
|
<p>发送http patch请求。(已废弃:使用SendRequest)</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
|||||||
@@ -49,8 +49,8 @@ func TestHttpPost(t *testing.T) {
|
|||||||
func TestHttpPostFormData(t *testing.T) {
|
func TestHttpPostFormData(t *testing.T) {
|
||||||
apiUrl := "https://jsonplaceholder.typicode.com/todos"
|
apiUrl := "https://jsonplaceholder.typicode.com/todos"
|
||||||
header := map[string]string{
|
header := map[string]string{
|
||||||
// "Content-Type": "application/x-www-form-urlencoded",
|
"Content-Type": "application/x-www-form-urlencoded",
|
||||||
"Content-Type": "multipart/form-data",
|
// "Content-Type": "multipart/form-data",
|
||||||
}
|
}
|
||||||
|
|
||||||
postData := url.Values{}
|
postData := url.Values{}
|
||||||
@@ -61,7 +61,7 @@ func TestHttpPostFormData(t *testing.T) {
|
|||||||
// postData["userId"] = "1"
|
// postData["userId"] = "1"
|
||||||
// postData["title"] = "title"
|
// postData["title"] = "title"
|
||||||
|
|
||||||
resp, err := HttpPost(apiUrl, header, postData, nil)
|
resp, err := HttpPost(apiUrl, header, nil, postData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,9 @@ package netutil
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -72,52 +74,34 @@ func setHeaderAndQueryParam(req *http.Request, reqUrl string, header, queryParam
|
|||||||
}
|
}
|
||||||
|
|
||||||
func setHeaderAndQueryAndBody(req *http.Request, reqUrl string, header, queryParam, body any) error {
|
func setHeaderAndQueryAndBody(req *http.Request, reqUrl string, header, queryParam, body any) error {
|
||||||
err := setHeader(req, header)
|
if err := setHeader(req, header); err != nil {
|
||||||
if err != nil {
|
return err
|
||||||
|
} else if err = setQueryParam(req, reqUrl, queryParam); err != nil {
|
||||||
|
return err
|
||||||
|
} else if err = setBodyByte(req, body); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
err = setQueryParam(req, reqUrl, queryParam)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func setHeader(req *http.Request, header any) error {
|
func setHeader(req *http.Request, header any) error {
|
||||||
if header != nil {
|
if header == nil {
|
||||||
switch v := header.(type) {
|
return nil
|
||||||
case map[string]string:
|
}
|
||||||
for k := range v {
|
|
||||||
req.Header.Add(k, v[k])
|
switch v := header.(type) {
|
||||||
}
|
case map[string]string:
|
||||||
case http.Header:
|
for k := range v {
|
||||||
for k, vv := range v {
|
req.Header.Add(k, v[k])
|
||||||
for _, vvv := range vv {
|
|
||||||
req.Header.Add(k, vvv)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
return errors.New("header params type should be http.Header or map[string]string")
|
|
||||||
}
|
}
|
||||||
|
case http.Header:
|
||||||
|
for k, vv := range v {
|
||||||
|
for _, vvv := range vv {
|
||||||
|
req.Header.Add(k, vvv)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
return errors.New("header params type should be http.Header or map[string]string")
|
||||||
}
|
}
|
||||||
|
|
||||||
if host := req.Header.Get("Host"); host != "" {
|
if host := req.Header.Get("Host"); host != "" {
|
||||||
@@ -137,19 +121,21 @@ func setUrl(req *http.Request, reqUrl string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func setQueryParam(req *http.Request, reqUrl string, queryParam any) error {
|
func setQueryParam(req *http.Request, reqUrl string, queryParam any) error {
|
||||||
|
if queryParam == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
var values url.Values
|
var values url.Values
|
||||||
if queryParam != nil {
|
switch v := queryParam.(type) {
|
||||||
switch v := queryParam.(type) {
|
case map[string]string:
|
||||||
case map[string]string:
|
values = url.Values{}
|
||||||
values = url.Values{}
|
for k := range v {
|
||||||
for k := range v {
|
values.Set(k, v[k])
|
||||||
values.Set(k, v[k])
|
|
||||||
}
|
|
||||||
case url.Values:
|
|
||||||
values = v
|
|
||||||
default:
|
|
||||||
return errors.New("query string params type should be url.Values or map[string]string")
|
|
||||||
}
|
}
|
||||||
|
case url.Values:
|
||||||
|
values = v
|
||||||
|
default:
|
||||||
|
return errors.New("query string params type should be url.Values or map[string]string")
|
||||||
}
|
}
|
||||||
|
|
||||||
// set url
|
// set url
|
||||||
@@ -170,14 +156,36 @@ func setQueryParam(req *http.Request, reqUrl string, queryParam any) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func setBodyByte(req *http.Request, body any) error {
|
func setBodyByte(req *http.Request, body any) error {
|
||||||
if body != nil {
|
if body == nil {
|
||||||
switch b := body.(type) {
|
return nil
|
||||||
case []byte:
|
}
|
||||||
req.Body = io.NopCloser(bytes.NewReader(b))
|
var bodyReader *bytes.Reader
|
||||||
req.ContentLength = int64(len(b))
|
switch b := body.(type) {
|
||||||
default:
|
case io.Reader:
|
||||||
return errors.New("body type should be []byte")
|
buf := bytes.NewBuffer(nil)
|
||||||
|
if _, err := io.Copy(buf, b); err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
req.Body = ioutil.NopCloser(buf)
|
||||||
|
req.ContentLength = int64(buf.Len())
|
||||||
|
case []byte:
|
||||||
|
bodyReader = bytes.NewReader(b)
|
||||||
|
req.Body = ioutil.NopCloser(bodyReader)
|
||||||
|
req.ContentLength = int64(bodyReader.Len())
|
||||||
|
case map[string]interface{}:
|
||||||
|
values := url.Values{}
|
||||||
|
for k := range b {
|
||||||
|
values.Set(k, fmt.Sprintf("%v", b[k]))
|
||||||
|
}
|
||||||
|
bodyReader = bytes.NewReader([]byte(values.Encode()))
|
||||||
|
req.Body = ioutil.NopCloser(bodyReader)
|
||||||
|
req.ContentLength = int64(bodyReader.Len())
|
||||||
|
case url.Values:
|
||||||
|
bodyReader = bytes.NewReader([]byte(b.Encode()))
|
||||||
|
req.Body = ioutil.NopCloser(bodyReader)
|
||||||
|
req.ContentLength = int64(bodyReader.Len())
|
||||||
|
default:
|
||||||
|
return fmt.Errorf("invalid body type: %T", b)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user