-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpreq.go
237 lines (207 loc) · 6.49 KB
/
httpreq.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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package kit
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"mime/multipart"
"net/http"
)
type contentType struct {
MIMEApplicationJSON string
MIMEApplicationJSONCharsetUTF8 string
MIMEApplicationJavaScript string
MIMEApplicationJavaScriptCharsetUTF8 string
MIMEApplicationXML string
MIMEApplicationXMLCharsetUTF8 string
MIMETextXML string
MIMETextXMLCharsetUTF8 string
MIMEApplicationForm string
MIMEApplicationFormUTF8 string
MIMEApplicationProtobuf string
MIMEApplicationMsgpack string
MIMETextHTML string
MIMETextHTMLCharsetUTF8 string
MIMETextPlain string
MIMETextPlainCharsetUTF8 string
MIMEMultipartForm string
MIMEOctetStream string
}
const (
charsetUTF8 = "charset=UTF-8"
)
type Header struct {
ContentType string
Token string
}
var ContentType = contentType{
MIMEApplicationJSON: "application/json",
MIMEApplicationJSONCharsetUTF8: "application/json" + "; " + charsetUTF8,
MIMEApplicationJavaScript: "application/javascript",
MIMEApplicationJavaScriptCharsetUTF8: "application/javascript" + "; " + charsetUTF8,
MIMEApplicationXML: "application/xml",
MIMEApplicationXMLCharsetUTF8: "application/xml" + "; " + charsetUTF8,
MIMETextXML: "text/xml",
MIMETextXMLCharsetUTF8: "text/xml" + "; " + charsetUTF8,
MIMEApplicationForm: "application/x-www-form-urlencoded",
MIMEApplicationFormUTF8: "application/x-www-form-urlencoded" + "; " + charsetUTF8,
MIMEApplicationProtobuf: "application/protobuf",
MIMEApplicationMsgpack: "application/msgpack",
MIMETextHTML: "text/html",
MIMETextHTMLCharsetUTF8: "text/html" + "; " + charsetUTF8,
MIMETextPlain: "text/plain",
MIMETextPlainCharsetUTF8: "text/plain" + "; " + charsetUTF8,
MIMEMultipartForm: "multipart/form-data",
MIMEOctetStream: "application/octet-stream",
}
func POST(token, url string, param interface{}, v interface{}) error {
b, err := json.Marshal(param)
if err != nil {
return err
}
httpReq, err := http.NewRequest("POST", url, bytes.NewBuffer(b))
httpReq.Header.Set("Content-Type", "application/json")
if token != "" {
httpReq.Header.Set("Authorization", "Bearer "+token)
}
resp, err := (&http.Client{}).Do(httpReq)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
b, _ := ioutil.ReadAll(resp.Body)
return fmt.Errorf("[%d %s]%s", resp.StatusCode, resp.Status, string(b))
}
if v != nil {
dec := json.NewDecoder(resp.Body)
if err := dec.Decode(&v); err != nil {
return err
}
}
return nil
}
func GET(token, url string, v interface{}) error {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return fmt.Errorf("HTTP New Request Error: %s", err)
}
if token != "" {
req.Header.Set("Authorization", "Bearer "+token)
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("HTTP Request Error: %s", err)
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
b, _ := ioutil.ReadAll(resp.Body)
return fmt.Errorf("[%d %s]%s", resp.StatusCode, resp.Status, string(b))
}
if v != nil {
dec := json.NewDecoder(resp.Body)
if err := dec.Decode(&v); err != nil {
return err
}
}
return nil
}
func NewPost(url string, param []byte, header *Header, transport *http.Transport) ([]byte, error) {
httpReq, err := http.NewRequest("POST", url, bytes.NewBuffer(param))
if header != nil {
httpReq.Header.Set("Content-Type", header.ContentType)
if header.Token != "" {
httpReq.Header.Set("Authorization", "Bearer "+header.Token)
}
} else {
httpReq.Header.Set("Content-Type", ContentType.MIMEApplicationFormUTF8)
}
var client *http.Client
if transport != nil {
client = &http.Client{Transport: transport}
} else {
client = &http.Client{}
}
resp, err := client.Do(httpReq)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
b, _ := ioutil.ReadAll(resp.Body)
return nil, fmt.Errorf("[%d %s]%s", resp.StatusCode, resp.Status, string(b))
}
respData, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return respData, nil
}
func NewGet(url string, header *Header, transport *http.Transport) ([]byte, error) {
httpReq, err := http.NewRequest("GET", url, nil)
if header != nil {
httpReq.Header.Set("Content-Type", header.ContentType)
if header.Token != "" {
httpReq.Header.Set("Authorization", "Bearer "+header.Token)
}
} else {
httpReq.Header.Set("Content-Type", ContentType.MIMEApplicationFormUTF8)
}
var client *http.Client
if transport != nil {
client = &http.Client{Transport: transport}
} else {
client = &http.Client{}
}
resp, err := client.Do(httpReq)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
b, _ := ioutil.ReadAll(resp.Body)
return nil, fmt.Errorf("[%d %s]%s", resp.StatusCode, resp.Status, string(b))
}
respData, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return respData, nil
}
type FileItem struct {
Key string //image_content
FileName string //test.jpg
Content []byte //[]byte
}
func NewPostFile(url string, paramTexts map[string]interface{}, paramFile FileItem) ([]byte, error) {
// if paramFiles ==nil {
// return NewPost(url,paramTexts,header,transport)
// }
bodyBuf := &bytes.Buffer{}
bodyWriter := multipart.NewWriter(bodyBuf)
for k, v := range paramTexts {
bodyWriter.WriteField(k, v.(string))
}
fileWriter, err := bodyWriter.CreateFormFile(paramFile.Key, paramFile.FileName)
if err != nil {
return nil, err
}
fileWriter.Write(paramFile.Content)
contentType := bodyWriter.FormDataContentType()
bodyWriter.Close()
resp, err := http.Post(url, contentType, bodyBuf)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
b, _ := ioutil.ReadAll(resp.Body)
return nil, fmt.Errorf("[%d %s]%s", resp.StatusCode, resp.Status, string(b))
}
respData, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return respData, nil
}