This repository has been archived by the owner on Dec 14, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
99 lines (80 loc) · 1.88 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package nethooks
import (
"bytes"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
log "github.com/Sirupsen/logrus"
)
func httpGet(url string, jdata interface{}) error {
r, err := http.Get(url)
if err != nil {
return err
}
defer r.Body.Close()
switch {
case r.StatusCode == int(404):
return errors.New("Page not found!")
case r.StatusCode == int(403):
return errors.New("Access denied!")
case r.StatusCode != int(200):
log.Debugf("GET Status '%s' status code %d \n", r.Status, r.StatusCode)
return errors.New(r.Status)
}
response, err := ioutil.ReadAll(r.Body)
if err != nil {
return err
}
if err := json.Unmarshal(response, jdata); err != nil {
return err
}
return nil
}
func httpDelete(url string) error {
req, err := http.NewRequest("DELETE", url, nil)
r, err := http.DefaultClient.Do(req)
if err != nil {
panic(err)
}
defer r.Body.Close()
// body, _ := ioutil.ReadAll(r.Body)
switch {
case r.StatusCode == int(404):
// return errors.New("Page not found!")
return nil
case r.StatusCode == int(403):
return errors.New("Access denied!")
case r.StatusCode != int(200):
log.Debugf("DELETE Status '%s' status code %d \n", r.Status, r.StatusCode)
return errors.New(r.Status)
}
return nil
}
func httpPost(url string, jdata interface{}) error {
buf, err := json.Marshal(jdata)
if err != nil {
return err
}
body := bytes.NewBuffer(buf)
r, err := http.Post(url, "application/json", body)
if err != nil {
return err
}
defer r.Body.Close()
switch {
case r.StatusCode == int(404):
return errors.New("Page not found!")
case r.StatusCode == int(403):
return errors.New("Access denied!")
case r.StatusCode != int(200):
log.Debugf("POST Status '%s' status code %d \n", r.Status, r.StatusCode)
return errors.New(r.Status)
}
response, err := ioutil.ReadAll(r.Body)
if err != nil {
return err
}
log.Debugf(string(response))
return nil
}