-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathhttp_utils.go
More file actions
190 lines (160 loc) · 5.47 KB
/
http_utils.go
File metadata and controls
190 lines (160 loc) · 5.47 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package exapi
//http request 工具函数
import (
"errors"
"fmt"
jsoniter "github.com/json-iterator/go"
"io/ioutil"
"net/http"
"net/url"
"strings"
)
var json = jsoniter.ConfigCompatibleWithStandardLibrary
func NewHttpRequest(client *http.Client, reqType string, reqUrl string, postData string, requstHeaders map[string]string) ([]byte, error) {
req, err := http.NewRequest(reqType, reqUrl, strings.NewReader(postData))
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36")
if requstHeaders != nil {
for k, v := range requstHeaders {
req.Header.Add(k, v)
}
}
resp, err := client.Do(req)
if err != nil {
Error("http request failed, url:%s, err:%s", reqUrl, err)
return nil, err
}
defer resp.Body.Close()
bodyData, err := ioutil.ReadAll(resp.Body)
if err != nil {
Error("read http body failed, url:%s, err:%s", reqUrl, err)
return nil, err
}
if resp.StatusCode != 200 {
return nil, errors.New(fmt.Sprintf("HttpStatusCode:%d ,Desc:%s", resp.StatusCode, string(bodyData)))
}
return bodyData, nil
}
func HttpGet(client *http.Client, reqUrl string) (map[string]interface{}, error) {
respData, err := NewHttpRequest(client, "GET", reqUrl, "", nil)
if err != nil {
return nil, err
}
var bodyDataMap map[string]interface{}
err = json.Unmarshal(respData, &bodyDataMap)
if err != nil {
return nil, err
}
return bodyDataMap, nil
}
func HttpGet2(client *http.Client, reqUrl string, headers map[string]string) (map[string]interface{}, error) {
if headers == nil {
headers = map[string]string{}
}
headers["Content-Type"] = "application/x-www-form-urlencoded"
respData, err := NewHttpRequest(client, "GET", reqUrl, "", headers)
if err != nil {
return nil, err
}
var bodyDataMap map[string]interface{}
err = json.Unmarshal(respData, &bodyDataMap)
if err != nil {
return nil, err
}
return bodyDataMap, nil
}
func HttpGet3(client *http.Client, reqUrl string, headers map[string]string) ([]interface{}, error) {
if headers == nil {
headers = map[string]string{}
}
headers["Content-Type"] = "application/x-www-form-urlencoded"
respData, err := NewHttpRequest(client, "GET", reqUrl, "", headers)
if err != nil {
return nil, err
}
var bodyDataMap []interface{}
err = json.Unmarshal(respData, &bodyDataMap)
if err != nil {
return nil, err
}
return bodyDataMap, nil
}
func HttpGet4(client *http.Client, reqUrl string, headers map[string]string, result interface{}) error {
if headers == nil {
headers = map[string]string{}
}
headers["Content-Type"] = "application/x-www-form-urlencoded"
respData, err := NewHttpRequest(client, "GET", reqUrl, "", headers)
if err != nil {
return err
}
err = json.Unmarshal(respData, result)
if err != nil {
return err
}
return nil
}
func HttpGet5(client *http.Client, reqUrl string, headers map[string]string) ([]byte, error) {
if headers == nil {
headers = map[string]string{}
}
headers["Content-Type"] = "application/x-www-form-urlencoded"
respData, err := NewHttpRequest(client, "GET", reqUrl, "", headers)
if err != nil {
return nil, err
}
return respData, nil
}
func HttpPostForm(client *http.Client, reqUrl string, postData url.Values) ([]byte, error) {
headers := map[string]string{"Content-Type": "application/x-www-form-urlencoded"}
return NewHttpRequest(client, "POST", reqUrl, postData.Encode(), headers)
}
func HttpPostForm2(client *http.Client, reqUrl string, postData url.Values, headers map[string]string) ([]byte, error) {
if headers == nil {
headers = map[string]string{}
}
headers["Content-Type"] = "application/x-www-form-urlencoded"
return NewHttpRequest(client, "POST", reqUrl, postData.Encode(), headers)
}
func HttpPostForm3(client *http.Client, reqUrl string, postData string, headers map[string]string) ([]byte, error) {
return NewHttpRequest(client, "POST", reqUrl, postData, headers)
}
func HttpPostForm4(client *http.Client, reqUrl string, postData map[string]string, headers map[string]string) ([]byte, error) {
if headers == nil {
headers = map[string]string{}
}
headers["Content-Type"] = "application/json"
data, _ := json.Marshal(postData)
return NewHttpRequest(client, "POST", reqUrl, string(data), headers)
}
func HttpPostForm5(client *http.Client, reqUrl string, postData string, headers map[string]string) ([]byte, error) {
if headers == nil {
headers = map[string]string{}
}
headers["Content-Type"] = "application/x-www-form-urlencoded"
return NewHttpRequest(client, "POST", reqUrl, postData, headers)
}
func HttpPostForm6(client *http.Client, reqUrl string, postData map[string]interface{}, headers map[string]string) ([]byte, error) {
if headers == nil {
headers = map[string]string{}
}
headers["Content-Type"] = "application/json"
data, _ := json.Marshal(postData)
return NewHttpRequest(client, "POST", reqUrl, string(data), headers)
}
func HttpDeleteForm(client *http.Client, reqUrl string, postData url.Values, headers map[string]string) ([]byte, error) {
if headers == nil {
headers = map[string]string{}
}
headers["Content-Type"] = "application/x-www-form-urlencoded"
return NewHttpRequest(client, "DELETE", reqUrl, postData.Encode(), headers)
}
func HttpDeleteForm2(client *http.Client, reqUrl string, postData string, headers map[string]string) ([]byte, error) {
if headers == nil {
headers = map[string]string{}
}
headers["Content-Type"] = "application/x-www-form-urlencoded"
return NewHttpRequest(client, "DELETE", reqUrl, postData, headers)
}