-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhttp.go
70 lines (54 loc) · 1.24 KB
/
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
64
65
66
67
68
69
70
// Functions to simplify HTTP requests
package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
)
func httpGet(url string, headers map[string]string) ([]byte, error) {
var (
body []byte
resp *http.Response
)
client := &http.Client{}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return body, err
}
for name, value := range headers {
req.Header.Add(name, value)
}
resp, err = client.Do(req)
if err != nil {
return body, err
}
if resp.StatusCode != 200 {
errormsg := "Error during HTTP GET to OpsGenie API: %d %s"
return body, errors.New(fmt.Sprintf(errormsg, resp.StatusCode, http.StatusText(resp.StatusCode)))
}
body, err = ioutil.ReadAll(resp.Body)
if err != nil {
return body, err
}
err = resp.Body.Close()
return body, err
}
func httpPostJSON(url string, data interface{}, headers map[string]string) error {
var slackClient = http.Client{}
newProfileJSON, err := json.Marshal(data)
if err != nil {
return err
}
req, reqErr := http.NewRequest(http.MethodPost, url, bytes.NewBuffer(newProfileJSON))
if reqErr != nil {
return reqErr
}
for name, value := range headers {
req.Header.Add(name, value)
}
_, postErr := slackClient.Do(req)
return postErr
}