forked from vmware/singleton
/
httpclient.go
49 lines (40 loc) · 915 Bytes
/
httpclient.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
/*
* Copyright 2020-2023 VMware, Inc.
* SPDX-License-Identifier: EPL-2.0
*/
package sgtn
import (
"fmt"
"io/ioutil"
"net/http"
"github.com/pkg/errors"
)
const (
servertimeout = 10
)
var (
httpclient *http.Client
newHTTPRequest = http.NewRequest
)
func httpget(urlToGet string, header map[string]string, body *[]byte) (*http.Response, error) {
logger.Info(fmt.Sprintf("Get URL %s with headers %v", urlToGet, header))
req, err := newHTTPRequest(http.MethodGet, urlToGet, nil)
if err != nil {
return nil, errors.WithStack(err)
}
req.Close = true
for k, v := range header {
req.Header.Add(k, v)
}
resp, err := httpclient.Do(req)
if err != nil {
return nil, errors.WithStack(err)
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return resp, errors.WithStack(err)
}
*body = b
return resp, err
}