feat: 封装通用的 RequestGet and RequestPost
This commit is contained in:
parent
b57b32944a
commit
9d11619e48
@ -11,9 +11,90 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
|
"io"
|
||||||
"net/http"
|
"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(url string, vals url.Values, headers map[string]string) ([]byte, error) {
|
||||||
|
req, err := http.NewRequest("GET", url, strings.NewReader(vals.Encode()))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
for hd, val := range headers {
|
||||||
|
req.Header.Set(hd, val)
|
||||||
|
}
|
||||||
|
client := &http.Client{}
|
||||||
|
resp, err := client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
body, err := io.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return body, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 默认是 json 请求
|
||||||
|
func RequestPost(url string, vals url.Values, headers map[string]string) ([]byte, error) {
|
||||||
|
req, err := http.NewRequest("POST", url, strings.NewReader(vals.Encode()))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if len(headers) == 0 {
|
||||||
|
headers["Content-Type"] = "application/json"
|
||||||
|
}
|
||||||
|
for hd, val := range headers {
|
||||||
|
req.Header.Set(hd, val)
|
||||||
|
}
|
||||||
|
|
||||||
|
client := &http.Client{}
|
||||||
|
resp, err := client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
body, err := io.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return body, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 旧的方法,不建议使用
|
||||||
func RequestJson(url string, data []byte) (*http.Response, error) {
|
func RequestJson(url string, data []byte) (*http.Response, error) {
|
||||||
res, err := http.Post(url, "application/json", bytes.NewBuffer(data))
|
res, err := http.Post(url, "application/json", bytes.NewBuffer(data))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user