diff --git a/helper/http_helper.go b/helper/http_helper.go index a92ab26..5119f1b 100644 --- a/helper/http_helper.go +++ b/helper/http_helper.go @@ -14,43 +14,20 @@ import ( "io" "net/http" "net/url" - "strings" ) -// vals := url.Values{} -// vals.Add("name","zhaofan") -// vals.Add("age","22") -func RequestPostForm(url string, vals url.Values) ([]byte, error) { - resp, err := http.PostForm(url, vals) - if err != nil { - return nil, err - } - body, err := io.ReadAll(resp.Body) - if err != nil { - return nil, err - } - return body, nil -} - -func RequestPostJson(url string, data map[string]any) ([]byte, error) { - bd, err := json.Marshal(data) - if err != nil { - return nil, err - } - resp, err := http.Post(url, "application/json", bytes.NewReader(bd)) - if err != nil { - return nil, err - } - body, err := io.ReadAll(resp.Body) - if err != nil { - return nil, err - } - return body, nil -} - // 比较完整的 get 实现 func RequestGet(uri string, vals url.Values, headers map[string]string) ([]byte, error) { - req, err := http.NewRequest("GET", uri, strings.NewReader(vals.Encode())) + furl, err := url.Parse(uri) + if err != nil { + return nil, err + } + if furl.RawQuery != "" { + furl.RawQuery += "&" + vals.Encode() + } else { + furl.RawQuery = vals.Encode() + } + req, err := http.NewRequest("GET", furl.String(), nil) if err != nil { return nil, err } @@ -62,6 +39,7 @@ func RequestGet(uri string, vals url.Values, headers map[string]string) ([]byte, if err != nil { return nil, err } + defer resp.Body.Close() body, err := io.ReadAll(resp.Body) if err != nil { return nil, err diff --git a/helper/http_helper_test.go b/helper/http_helper_test.go new file mode 100644 index 0000000..c5fb5f9 --- /dev/null +++ b/helper/http_helper_test.go @@ -0,0 +1,57 @@ +// +// http_helper_test.go +// Copyright (C) 2023 tiglog +// +// Distributed under terms of the MIT license. +// + +package helper_test + +import ( + "fmt" + "net/url" + "testing" + + "git.hexq.cn/tiglog/golib/gtest" + "git.hexq.cn/tiglog/golib/helper" +) + +func TestRequestGet(t *testing.T) { + headers := map[string]string{ + "Authorization": "Bearer foo", + } + uri := "http://tapp.cmoodle.com/api/query?aa=11" + vals := url.Values{} + vals.Set("foo", "bar") + vals.Set("hello", "世界") + res, err := helper.RequestGet(uri, vals, headers) + gtest.NoError(t, err) + gtest.NotEqual(t, "{}", string(res)) +} + +func TestRequestPost(t *testing.T) { + headers := map[string]string{ + "Authorization": "Bearer foo", + "Content-Type": "application/json", + } + uri := "http://tapp.cmoodle.com/api/post/data?aa=11" + data := map[string]any{} + data["foo"] = "bar" + data["hello"] = "🌐世界1" + res, err := helper.RequestPost(uri, data, headers) + gtest.NoError(t, err) + fmt.Println(string(res)) + + headers = map[string]string{ + "Authorization": "Bearer foo", + "Content-Type": "application/x-www-form-urlencoded", + } + uri = "http://tapp.cmoodle.com/api/post/form?aa=11" + data = map[string]any{} + data["foo"] = "bar" + data["hello"] = "🌐世界2" + res2, err := helper.RequestPost(uri, data, headers) + gtest.NoError(t, err) + fmt.Println(string(res2)) + +}