chore: request data 的值可以是任意格式
This commit is contained in:
parent
9d11619e48
commit
27715b7b27
@ -14,7 +14,6 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// vals := url.Values{}
|
// vals := url.Values{}
|
||||||
@ -49,8 +48,12 @@ func RequestPostJson(url string, data map[string]any) ([]byte, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 比较完整的 get 实现
|
// 比较完整的 get 实现
|
||||||
func RequestGet(url string, vals url.Values, headers map[string]string) ([]byte, error) {
|
func RequestGet(url string, data map[string]any, headers map[string]string) ([]byte, error) {
|
||||||
req, err := http.NewRequest("GET", url, strings.NewReader(vals.Encode()))
|
bd, err := json.Marshal(data)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
req, err := http.NewRequest("GET", url, bytes.NewBuffer(bd))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -70,8 +73,12 @@ func RequestGet(url string, vals url.Values, headers map[string]string) ([]byte,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 默认是 json 请求
|
// 默认是 json 请求
|
||||||
func RequestPost(url string, vals url.Values, headers map[string]string) ([]byte, error) {
|
func RequestPost(url string, data map[string]any, headers map[string]string) ([]byte, error) {
|
||||||
req, err := http.NewRequest("POST", url, strings.NewReader(vals.Encode()))
|
bd, err := json.Marshal(data)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
req, err := http.NewRequest("POST", url, bytes.NewBuffer(bd))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user