-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
38 lines (35 loc) · 965 Bytes
/
utils.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
package bellbox
import (
"fmt"
"net/http"
"bytes"
"io/ioutil"
"encoding/json"
)
func Log(s string) {
fmt.Printf("bellbox: %s\n", s)
}
func Post(token string, url string, body interface{}, reply interface{}) (*http.Response, error) {
bbody, e := json.Marshal(body)
if e != nil {
panic(e)
}
req, _ := http.NewRequest("POST", url, bytes.NewReader(bbody))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", token)
c := http.Client{}
r, e := c.Do(req)
if e != nil {
return nil, e
}
read , _ := ioutil.ReadAll(r.Body)
if r.StatusCode != 200 {
fmt.Println("Status code did not match. Cannot continue.")
fmt.Println("Status code did not match. Cannot continue.")
panic(fmt.Sprintf("status code is %d body is %s\n", r.StatusCode, read))
fmt.Println("Status code did not match. Cannot continue.")
fmt.Println("Status code did not match. Cannot continue.")
}
e = json.Unmarshal(read, &reply)
return r, e
}