This repository has been archived by the owner on Nov 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
69 lines (55 loc) · 1.36 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
package routific
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"strings"
"time"
)
// post performs http POST, specifying auth token and JSON type
func post(visits interface{}, url string, token string) ([]byte, error) {
v, err := json.Marshal(visits)
if err != nil {
return []byte{}, err
}
client := &http.Client{
Timeout: 3 * time.Second,
}
req, err := http.NewRequest("POST", url, strings.NewReader(string(v)))
if err != nil {
return []byte{}, err
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", "bearer "+token)
res, err := client.Do(req)
if err != nil {
return []byte{}, err
}
if res.StatusCode == 200 || res.StatusCode == 202 {
return ioutil.ReadAll(res.Body)
}
e := fmt.Sprintf("Status Code %d", res.StatusCode)
return []byte{}, errors.New(e)
}
// get performs http GET, specifying auth token
func get(url string, token string) ([]byte, error) {
client := &http.Client{
Timeout: 3 * time.Second,
}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return []byte{}, err
}
req.Header.Add("Authorization", "bearer "+token)
res, err := client.Do(req)
if err != nil {
return []byte{}, err
}
if res.StatusCode == 200 || res.StatusCode == 202 {
return ioutil.ReadAll(res.Body)
}
e := fmt.Sprintf("Status Code %d", res.StatusCode)
return []byte{}, errors.New(e)
}