-
Notifications
You must be signed in to change notification settings - Fork 0
/
http-utils.go
83 lines (72 loc) · 2.15 KB
/
http-utils.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package main
import (
"crypto/tls"
"crypto/x509"
"fmt"
"io"
"log"
"net/http"
"net/url"
)
// JSONApplication for Content-Type headers
const JSONApplication string = "application/json"
// JSONApplicationUTF8 for Content-Type headers, UTF charset
const JSONApplicationUTF8 string = JSONApplication + "; charset=UTF-8"
// TextPlain for Content-Type headers
const TextPlain string = "text/plain"
// TextPlainUTF8 for Content-Type headers, UTF charset
const TextPlainUTF8 string = TextPlain + "; charset=UTF-8"
// ContentType for header key
const ContentType string = "Content-Type"
// UserAgent for header key
const UserAgent string = "User-Agent"
// Authorization for header key
const Authorization string = "Authorization"
// osctrlUserAgent for customized User-Agent
const osctrlUserAgent string = "osctrld-http-client/" + OsctrldVersion
// SendRequest - Helper function to send HTTP requests
func SendRequest(reqType, reqURL string, params io.Reader, headers map[string]string, insecure bool) (int, []byte, error) {
u, err := url.Parse(reqURL)
if err != nil {
return 0, nil, fmt.Errorf("invalid url: %v", err)
}
client := &http.Client{}
if u.Scheme == "https" {
certPool, err := x509.SystemCertPool()
if err != nil {
return 0, nil, fmt.Errorf("error loading x509 certificate pool: %v", err)
}
tlsCfg := &tls.Config{RootCAs: certPool}
if insecure {
tlsCfg.InsecureSkipVerify = true
}
client.Transport = &http.Transport{TLSClientConfig: tlsCfg}
}
req, err := http.NewRequest(reqType, reqURL, params)
if err != nil {
return 0, []byte("Cound not prepare request"), err
}
// Set custom User-Agent
req.Header.Set(UserAgent, osctrlUserAgent)
// Prepare headers
for key, value := range headers {
req.Header.Add(key, value)
}
// Send request
resp, err := client.Do(req)
if err != nil {
return 0, []byte("Error sending request"), err
}
//defer resp.Body.Close()
defer func() {
if err := resp.Body.Close(); err != nil {
log.Printf("Failed to close body %v", err)
}
}()
// Read body
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
return 0, []byte("Can not read response"), err
}
return resp.StatusCode, bodyBytes, nil
}