-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_request.go
47 lines (38 loc) · 913 Bytes
/
http_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
package wechatpay
import (
"bytes"
"crypto/tls"
"encoding/xml"
"io/ioutil"
"net/http"
)
func PostXML(url string, req XmlMap) ([]byte, error) {
reqBody, err := xml.Marshal(req)
if err != nil {
return nil, err
}
response, err := http.Post(url, "application/xml; charset=utf-8", bytes.NewReader(reqBody))
if err != nil {
return nil, err
}
defer response.Body.Close()
return ioutil.ReadAll(response.Body)
}
func PostXMLOverTLS(url string, tlsConfig *tls.Config, req XmlMap) ([]byte, error) {
reqBuffer, err := xml.Marshal(req)
if err != nil {
return nil, err
}
tlsClient := http.Client{}
if tlsConfig != nil {
tlsClient.Transport = &http.Transport{
TLSClientConfig: tlsConfig,
}
}
resp, err := tlsClient.Post(url, "application/xml; charset=utf-8", bytes.NewBuffer(reqBuffer))
if err != nil {
return nil, err
}
defer resp.Body.Close()
return ioutil.ReadAll(resp.Body)
}