-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathclients.go
61 lines (54 loc) · 1.24 KB
/
clients.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package main
import (
"crypto/tls"
"fmt"
"net/http"
"strings"
"time"
)
func makeHTTPRequest(url string, headers map[string]string, body string, method string) (*http.Response, error) {
client := http.Client{
Timeout: 60 * time.Second,
CheckRedirect: func(_ *http.Request, via []*http.Request) error {
// Allow up to 10 redirects
if len(via) >= 10 {
return fmt.Errorf("too many redirects")
}
return nil
},
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, // Disable SSL certificate verification
},
}
var req *http.Request
var err error
switch method {
case http.MethodGet:
req, err = http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, err
}
case http.MethodPost:
if body != "" {
req, err = http.NewRequest(http.MethodPost, url, strings.NewReader(body))
if err != nil {
return nil, err
}
} else {
req, err = http.NewRequest(http.MethodPost, url, nil)
if err != nil {
return nil, err
}
}
default:
return nil, fmt.Errorf("unsupported request method encountered")
}
for key, value := range headers {
req.Header.Set(key, value)
}
response, err := client.Do(req)
if err != nil {
return nil, err
}
return response, nil
}