diff --git a/http.go b/http.go index ba760aa..564ff8e 100644 --- a/http.go +++ b/http.go @@ -10,6 +10,37 @@ import ( "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 请求 func HttpPost(link string, header map[string]string, json []byte) ([]byte, error) { client := &http.Client{Timeout: 20 * time.Second}