-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
64 lines (52 loc) · 1.07 KB
/
config.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
package request
import (
"io"
"net/http"
"net/url"
)
// Config 请求配置
type Config struct {
// header 头
Headers http.Header
// 请求地址
URL string
// 请求方法
Method string
// 请求 query 或者 search 参数
Params url.Values
// 请求主体
Data io.Reader
}
// NewConfig 新建 config 实例
func NewConfig(rawurl string, method ...string) *Config {
mth := http.MethodGet
if method != nil && len(method) > 0 {
mth = method[0]
}
return &Config{
URL: rawurl,
Method: mth,
Headers: make(http.Header),
Params: make(url.Values),
}
}
// SetContentType 添加 content-type
func (c *Config) SetContentType(val string) *Config {
c.Headers.Add("Content-Type", val)
return c
}
// AddHeader 添加 header 头
func (c *Config) AddHeader(key, val string) *Config {
c.Headers.Add(key, val)
return c
}
// AddParam 添加 query 或者 search 参数
func (c *Config) AddParam(key, val string) *Config {
c.Params.Add(key, val)
return c
}
// SetData 设置请求主体
func (c *Config) SetData(dt io.Reader) *Config {
c.Data = dt
return c
}