/
http.go
63 lines (50 loc) · 913 Bytes
/
http.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
package net
import (
"bytes"
"crypto/tls"
"io"
"net/http"
)
type HttpClient struct {
client *http.Client
Skip bool
}
func (c *HttpClient) Init() *HttpClient {
if c.Skip {
c.client = &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
}
} else {
c.client = &http.Client{}
}
return c
}
func (c *HttpClient) Execute(method string, url string, body []byte, header http.Header) (data []byte, err error) {
var (
req *http.Request
res *http.Response
buf *bytes.Buffer
)
buf = bytes.NewBuffer([]byte{})
if body != nil {
buf = bytes.NewBuffer(body)
}
req, err = http.NewRequest(method, url, buf)
if err != nil {
return
}
if header != nil {
req.Header = header
}
res, err = c.client.Do(req)
if err != nil {
return
}
if res.Body != nil {
data, err = io.ReadAll(res.Body)
}
_ = res.Body.Close()
return
}