1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-11 16:22:26 +08:00

fix: fix body param bug when send post request

This commit is contained in:
dudaodong
2023-05-30 11:01:39 +08:00
parent 40ab5e8f7b
commit 3b1597d6f7
4 changed files with 125 additions and 111 deletions

View File

@@ -2,6 +2,7 @@ package netutil
import (
"encoding/json"
"io"
"io/ioutil"
"log"
"net/http"
@@ -51,23 +52,23 @@ func TestHttpPost(t *testing.T) {
func TestHttpPostFormData(t *testing.T) {
apiUrl := "https://jsonplaceholder.typicode.com/todos"
header := map[string]string{
// "Content-Type": "application/x-www-form-urlencoded",
"Content-Type": "multipart/form-data",
}
type Todo struct {
UserId int `json:"userId"`
Title string `json:"title"`
"Content-Type": "application/x-www-form-urlencoded",
// "Content-Type": "multipart/form-data",
}
postData := url.Values{}
postData.Add("userId", "1")
postData.Add("title", "TestAddToDo")
postData.Add("title", "TestToDo")
resp, err := HttpPost(apiUrl, header, postData, nil)
// postData := make(map[string]string)
// postData["userId"] = "1"
// postData["title"] = "title"
resp, err := HttpPost(apiUrl, header, nil, postData)
if err != nil {
log.Fatal(err)
t.FailNow()
}
body, _ := ioutil.ReadAll(resp.Body)
body, _ := io.ReadAll(resp.Body)
t.Log("response: ", resp.StatusCode, string(body))
}