-
Notifications
You must be signed in to change notification settings - Fork 0
/
post.go
133 lines (121 loc) · 3.48 KB
/
post.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
package Net
import (
"crypto/tls"
"time"
)
type Post struct {
curl Curl
InsecureSkipVerify bool
ret *response
err error
Timeout time.Duration
}
func (self Post) New() *Post {
return &self
}
func (self *Post) SetTimeOut(Timeout time.Duration) *Post {
self.Timeout = Timeout
return self
}
func (self *Post) AllowInsecure() *Post {
self.InsecureSkipVerify = true
return self
}
func (self *Post) PostRpc(url string, postData interface{}, username, password string) *Post {
req := self.curl.newRequest().request
self.curl.SetHeaderJson()
req.SetBasicAuth(username, password)
if self.Timeout != 0 {
req.SetTimeout(self.Timeout)
}
req.DisableKeepAlives(true)
req.SetTLSClient(&tls.Config{InsecureSkipVerify: self.InsecureSkipVerify})
self.ret, self.err = req.post(url, postData)
return self
}
func (self *Post) PostRaw(url string, postData interface{}) *Post {
req := self.curl.newRequest().request
self.curl.SetHeaderTextPlain()
if self.Timeout != 0 {
req.SetTimeout(self.Timeout)
}
req.DisableKeepAlives(true)
req.SetTLSClient(&tls.Config{InsecureSkipVerify: self.InsecureSkipVerify})
self.ret, self.err = req.post(url, postData)
return self
}
func (self *Post) PostFormData(url string, queries map[string]interface{}, postData map[string]string, headers map[string]string, cookies map[string]string) *Post {
req := self.curl.newRequest().request
self.curl.SetHeaderFormData()
req.SetHeaders(headers)
req.SetCookies(cookies)
if self.Timeout != 0 {
req.SetTimeout(self.Timeout)
}
req.DisableKeepAlives(true)
req.SetTLSClient(&tls.Config{InsecureSkipVerify: self.InsecureSkipVerify})
url, self.err = buildUrl(url, queries)
if self.err != nil {
return self
}
self.ret, self.err = req.postFD(url, postData)
return self
}
func (self *Post) PostUrlXEncode(url string, queries map[string]interface{}, postData map[string]interface{}, headers map[string]string, cookies map[string]string) *Post {
req := self.curl.newRequest().request
self.curl.SetHeaderUrlEncode()
req.SetHeaders(headers)
req.SetCookies(cookies)
if self.Timeout != 0 {
req.SetTimeout(self.Timeout)
}
req.DisableKeepAlives(true)
req.SetTLSClient(&tls.Config{InsecureSkipVerify: self.InsecureSkipVerify})
url, self.err = buildUrl(url, queries)
if self.err != nil {
return self
}
self.ret, self.err = req.post(url, postData)
return self
}
func (self *Post) PostJson(url string, queries map[string]interface{}, postData map[string]interface{}, headers map[string]string, cookies map[string]string) *Post {
req := self.curl.newRequest().request
self.curl.SetHeaderJson()
req.SetHeaders(headers)
req.SetCookies(cookies)
if self.Timeout != 0 {
req.SetTimeout(self.Timeout)
}
req.DisableKeepAlives(true)
req.SetTLSClient(&tls.Config{InsecureSkipVerify: self.InsecureSkipVerify})
url, self.err = buildUrl(url, queries)
if self.err != nil {
return self
}
self.ret, self.err = req.post(url, postData)
return self
}
func (self *Post) RetCookie() (cookie map[string]interface{}, err error) {
if self.err != nil {
return nil, self.err
}
return self.curl.cookieHandler(self.ret.Cookies()), nil
}
func (self *Post) RetString() (string, error) {
if self.err != nil {
return "", self.err
}
return self.ret.bodystring()
}
func (self *Post) RetBytes() ([]byte, error) {
if self.err != nil {
return nil, self.err
}
return self.ret.bodybytes()
}
func (self *Post) RetJson(v any) error {
if self.err != nil {
return self.err
}
return self.ret.bodyjson(v)
}