/
http_post_form.go
44 lines (37 loc) · 1 KB
/
http_post_form.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
package cmn
import (
"errors"
"io"
"net/http"
"strings"
)
// 使用标准包进行Post请求,固定Content-Type:application/x-www-form-urlencoded,其他自定义headers格式为 K:V
func HttpPostForm(url string, formMap map[string]string, headers ...string) ([]byte, error) {
sendBody := http.Request{}
sendBody.ParseForm()
for k, v := range formMap {
sendBody.Form.Add(k, v)
}
sendData := sendBody.Form.Encode()
client := &http.Client{}
request, err := http.NewRequest("POST", url, strings.NewReader(sendData))
if err != nil {
return nil, err
}
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
for i, max := 0, len(headers); i < max; i++ {
strs := Split(headers[i], ":")
if len(strs) > 1 {
request.Header.Set(Trim(strs[0]), Trim(strs[1]))
}
}
res, err := client.Do(request)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return nil, errors.New(IntToString(res.StatusCode))
}
return io.ReadAll(res.Body)
}