-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.go
59 lines (51 loc) · 1.11 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
package statuspageio
import (
"bytes"
"encoding/json"
"errors"
"io"
"log"
"net/http"
"strings"
)
func addHeaders(headers []Header, req *http.Request) {
for i := 0; i < len(headers); i++ {
req.Header.Add(headers[i].Name, headers[i].Value)
}
}
func (request RequestFormat) exec() (*http.Response, error) {
var body io.Reader
var header Header
switch request.Body.(type) {
default:
return nil, errors.New("Body type not recognized")
case string:
body = strings.NewReader(request.Body.(string))
header = Header{
Name: "Content-Type",
Value: "application/x-www-form-urlencoded",
}
request.Headers = append(request.Headers, header)
case incident:
bodyJSON, err := json.Marshal(request.Body)
if err != nil {
log.Fatal(err)
}
header = Header{
Name: "Content-Type",
Value: "application/json",
}
body = bytes.NewBuffer(bodyJSON)
}
req, err := http.NewRequest(request.Method, request.URL, body)
if err != nil {
log.Fatal(err)
}
addHeaders(request.Headers, req)
// Execute http request
res, err := request.Client.Do(req)
if err != nil {
log.Fatal(err)
}
return res, nil
}