|
@@ -70,3 +70,39 @@ func Post(reqUrl string, payload string, timeout time.Duration) ([]byte, error)
|
|
|
|
|
|
return b, nil
|
|
return b, nil
|
|
}
|
|
}
|
|
|
|
+func PostWithHeader(reqUrl string, payload string, headers map[string]string, timeout time.Duration) ([]byte, error) {
|
|
|
|
+ req, err := http.NewRequest(http.MethodPost, reqUrl, strings.NewReader(payload))
|
|
|
|
+ if err != nil {
|
|
|
|
+ logrus.Error(err)
|
|
|
|
+ return nil, err
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ for key, val := range headers {
|
|
|
|
+ req.Header.Set(key, val)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ cli := http.Client{
|
|
|
|
+ Timeout: timeout,
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ resp, err := cli.Do(req)
|
|
|
|
+ if err != nil {
|
|
|
|
+ logrus.Error(err)
|
|
|
|
+ return nil, err
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if resp.StatusCode != http.StatusOK {
|
|
|
|
+ logrus.Error("状态码错误:", resp)
|
|
|
|
+ return nil, errors.New("状态码错误")
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ defer resp.Body.Close()
|
|
|
|
+
|
|
|
|
+ b, err := io.ReadAll(resp.Body)
|
|
|
|
+ if err != nil {
|
|
|
|
+ logrus.Error(err)
|
|
|
|
+ return nil, err
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return b, nil
|
|
|
|
+}
|