http put
This commit is contained in:
parent
1cdf6cb507
commit
b8858451cb
31
http.go
31
http.go
|
|
@ -10,6 +10,37 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func HttpPut(link string, header map[string]string, json []byte) ([]byte, error) {
|
||||||
|
client := &http.Client{Timeout: 20 * time.Second}
|
||||||
|
//忽略https的证书
|
||||||
|
client.Transport = &http.Transport{
|
||||||
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
||||||
|
}
|
||||||
|
|
||||||
|
u, _ := url.Parse(link)
|
||||||
|
|
||||||
|
req, err := http.NewRequest("PUT", u.String(), bytes.NewBuffer(json))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
req.Header.Add("Content-Type", "application/json")
|
||||||
|
if header != nil {
|
||||||
|
for k, v := range header {
|
||||||
|
req.Header.Set(k, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
return nil, fmt.Errorf("%d:%s", resp.StatusCode, resp.Status)
|
||||||
|
}
|
||||||
|
return io.ReadAll(resp.Body)
|
||||||
|
}
|
||||||
|
|
||||||
// PostJson 请求
|
// PostJson 请求
|
||||||
func HttpPost(link string, header map[string]string, json []byte) ([]byte, error) {
|
func HttpPost(link string, header map[string]string, json []byte) ([]byte, error) {
|
||||||
client := &http.Client{Timeout: 20 * time.Second}
|
client := &http.Client{Timeout: 20 * time.Second}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue