-
Notifications
You must be signed in to change notification settings - Fork 11
/
http_request.go
75 lines (60 loc) · 1.45 KB
/
http_request.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
package PushSDK
import (
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
"time"
)
func postReqUrlencoded(requestPath string, forms map[string]string, header map[string]string) ([]byte, error) {
fmt.Println(requestPath, forms, header)
form := url.Values{}
for k, v := range forms {
form.Add(k, v)
}
param := form.Encode()
req, err := http.NewRequest("POST", fmt.Sprintf("%s", requestPath), strings.NewReader(param))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8")
if len(header) > 0 {
for k, v := range header {
req.Header.Add(k, fmt.Sprintf(v))
}
}
var client = &http.Client{
Timeout: 5 * time.Second,
}
res, err := client.Do(req)
body, err := ioutil.ReadAll(res.Body)
res.Body.Close()
if err != nil {
return body, err
}
return body, nil
}
func postReqJson(requestPath string, forms string, header map[string]string) ([]byte, error) {
fmt.Println(requestPath, forms, header)
req, err := http.NewRequest("POST", fmt.Sprintf("%s", requestPath), strings.NewReader(forms))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
if len(header) > 0 {
for k, v := range header {
req.Header.Add(k, fmt.Sprintf(v))
}
}
var client = &http.Client{
Timeout: 5 * time.Second,
}
res, err := client.Do(req)
body, err := ioutil.ReadAll(res.Body)
res.Body.Close()
if err != nil {
return body, err
}
return body, nil
}