-
Notifications
You must be signed in to change notification settings - Fork 4
/
http.go
44 lines (41 loc) · 1.08 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
package src
import (
"bytes"
"io/ioutil"
"net/http"
"strings"
)
func Http(method string, path string, param string) []byte {
client := &http.Client{}
req, err := http.NewRequest(method, URL+path, strings.NewReader(param))
if err != nil {
panic(err)
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
token, _ := ReadToken() //从文件取出数据
req.Header.Set("Authorization", "Bearer "+string(token))
resp, err := client.Do(req)
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
return body
}
func HttpJson(method string, path string, param []byte) []byte {
client := &http.Client{}
req, err := http.NewRequest(method, URL+path, bytes.NewBuffer(param))
if err != nil {
panic(err)
}
req.Header.Set("Content-Type", "application/json; charset=UTF-8")
token, _ := ReadToken() //从文件取出数据
req.Header.Set("Authorization", "Bearer "+string(token))
resp, err := client.Do(req)
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
return body
}