/
post.go
48 lines (45 loc) · 1.25 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
package request
import (
"encoding/json"
"net/http"
"strings"
)
// SimplePost Post
func SimplePost(requestUrl string, data map[string]string) (*HttpResultDto, error) {
return Post(requestUrl, NewOptions().WithData(data))
}
// Post Post with options
func Post(requestUrl string, options *Options) (*HttpResultDto, error) {
if options.Header == nil {
options.Header = &HeaderDto{UserAgent: HTTP_USER_AGENT}
}
var param string
if options.JsonData != nil || options.DataType == JSON {
if options.Header.ContentType == "" {
options.Header.ContentType = CONTENT_TYPE_JSON
}
var jsonData []byte
if options.JsonData != nil {
jsonData, _ = json.Marshal(options.JsonData)
} else if options.Data != nil {
jsonData, _ = json.Marshal(options.Data)
}
param = string(jsonData)
} else {
if options.Header.ContentType == "" {
options.Header.ContentType = CONTENT_TYPE_FORM
}
param = GetReqData(options.Data)
}
req, _ := http.NewRequest("POST", requestUrl, strings.NewReader(param))
if options.Query != nil {
q := req.URL.Query()
for k, v := range options.Query {
q.Add(k, v)
}
req.URL.RawQuery = q.Encode()
}
req = addHeader(req, options.Header)
req = addCookie(req, options.Cookie)
return request(req, options.Proxy, options.Timeout)
}