-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequests.go
More file actions
41 lines (38 loc) · 907 Bytes
/
requests.go
File metadata and controls
41 lines (38 loc) · 907 Bytes
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
package main
import (
"bytes"
"io/ioutil"
"net/http"
)
type request struct {
Method string
URL string
Payload []byte
}
func doSensuAPIRequest(request *request) ([]byte, int) {
// form request
req, _ := http.NewRequest(request.Method, request.URL, bytes.NewBuffer(request.Payload))
req.Header.Add("content-type", "application/x-www-form-urlencoded")
req.Header.Add("accept", "application/json")
req.Header.Add("cache-control", "no-cache")
// do request
res, err := http.DefaultClient.Do(req)
if err != nil {
handleError(err)
}
// read and close response
body, _ := ioutil.ReadAll(res.Body)
if res.StatusCode == 400 {
trowError("400 (Bad Request)")
}
if res.StatusCode == 404 {
trowError("404 (Not Found)")
}
if res.StatusCode == 500 {
trowError("500 (Internal Server Error)")
}
defer res.Body.Close()
result := body
// return result
return result, res.StatusCode
}