root 11 months ago
parent
commit
b9bb4793b2
1 changed files with 31 additions and 0 deletions
  1. 31 0
      webutils/http.go

+ 31 - 0
webutils/http.go

@@ -106,3 +106,34 @@ func PostWithHeader(reqUrl string, payload string, headers map[string]string, ti
 
 
 	return b, nil
 	return b, nil
 }
 }
+func GetWithHeader(reqUrl string, headers map[string]string, timeout time.Duration) ([]byte, error) {
+	req, err := http.NewRequest(http.MethodGet, reqUrl, nil)
+	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
+	}
+
+	defer resp.Body.Close()
+
+	b, err := io.ReadAll(resp.Body)
+	if err != nil {
+		logrus.Error(err)
+		return nil, err
+	}
+
+	return b, nil
+}