/
request.go
203 lines (176 loc) · 4.29 KB
/
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
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
package httpclient
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"mime/multipart"
"net/http"
"net/http/cookiejar"
"path/filepath"
"strings"
"sync"
)
type Request struct {
url string
method string
params map[string]interface{}
requestBody []byte
files []UploadFile
host string
header http.Header
contentType string
errorRaw string
timeout int
mutex sync.Mutex
cooJar *cookiejar.Jar
skipHttps bool
disableKeepAlives bool
cookieData map[string]*http.Cookie
}
// Header 设置header头信息
func (c *Request) Header(key, value string) *Request {
c.mutex.Lock()
defer c.mutex.Unlock()
c.header[key] = []string{value}
if strings.ToLower(key) == "content-type" {
c.contentType = value
}
return c
}
func (c *Request) POST(url string) *Request {
c.mutex.Lock()
defer c.mutex.Unlock()
c.method = "POST"
c.url = url
return c
}
func (c *Request) GET(url string) *Request {
c.mutex.Lock()
defer c.mutex.Unlock()
c.method = "GET"
c.url = url
return c
}
func (c *Request) ContentType(value string) *Request {
c.mutex.Lock()
defer c.mutex.Unlock()
c.header.Set("Content-Type", value)
c.contentType = value
return c
}
// WithHost 添加请求头host
func (c *Request) WithHost(host string) *Request {
c.host = host
return c
}
// WithFile 添加文件form-data方式发送
func (c *Request) AddFile(name string, filePath string) *Request {
c.files = append(c.files, UploadFile{name, filePath})
return c
}
// SkipHttps 跳过https证书校验
func (c *Request) SkipHttps() *Request {
c.skipHttps = true
return c
}
// DisableKeepAlives 关闭KeepAlives
func (c *Request) DisableKeepAlives() *Request {
c.disableKeepAlives = true
return c
}
// ClearParam 清空重置请求参数
func (c *Request) ClearParam() *Request {
c.requestBody = []byte{}
c.params = map[string]interface{}{}
return c
}
func (c *Request) SetTimeout(timeout int) *Request {
c.timeout = timeout
return c
}
// cookie保持 通过配置请求id将cookie保持
func (c *Request) WithCookie() *Request {
if c.cooJar == nil {
cooJar, _ := cookiejar.New(nil)
c.cooJar = cooJar
}
return c
}
func (c *Request) Error() error {
if c.errorRaw == "" {
return nil
}
return errors.New(c.errorRaw)
}
func (c *Request) setCookieData(name string, cookie *http.Cookie) {
c.mutex.Lock()
defer c.mutex.Unlock()
c.cookieData[name] = cookie
}
// 直接传递body中的参数
func (c *Request) WithBody(bodyStream string) *Request {
c.requestBody = []byte(bodyStream)
return c
}
// 参数设置 表单请求支持json和form两种类型
func (c *Request) FormParams(obj map[string]interface{}) *Request {
c.params = obj
return c
}
func (c *Request) paraseParams() *Request {
if len(c.files) > 0 {
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
for _, uploadFile := range c.files {
fileBytes, err := ioutil.ReadFile(uploadFile.Filepath)
if err != nil {
fmt.Printf("can not open file %s", uploadFile.Filepath)
continue
}
part, err := writer.CreateFormFile(uploadFile.Name, filepath.Base(uploadFile.Filepath))
if err != nil {
fmt.Printf("CreateFormFile error %v", err)
continue
}
part.Write(fileBytes)
}
for k, v := range c.params {
stringVal := fmt.Sprintf("%v", v)
if err := writer.WriteField(k, stringVal); err != nil {
fmt.Printf("WriteField error %v", err)
continue
}
}
c.contentType = writer.FormDataContentType()
if err := writer.Close(); err != nil {
panic(err)
}
c.requestBody = body.Bytes()
return c
}
if strings.Contains(c.contentType, "application/x-www-form-urlencoded") {
params := ""
val := make([]interface{}, 0, len(c.params))
for k, item := range c.params {
params += k + "=%v&"
val = append(val, item)
}
paramsLen := len(params) - 1
params = params[0:paramsLen]
params = fmt.Sprintf(params, val...)
c.requestBody = []byte(params)
return c
}
if strings.Contains(c.contentType, "application/json") {
params, err := json.Marshal(c.params)
if err != nil {
c.errorRaw += err.Error() + "|"
}
c.requestBody = params
return c
}
c.errorRaw += "Use params you must set Content-Type eq 'application/json' or 'application/x-www-form-urlencoded'"
return c
}