/
http.go
74 lines (65 loc) · 2.02 KB
/
http.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
package common
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
)
// Get http get
func Get(url string) (body []byte, err error) {
response, err := http.Get(url)
if err != nil {
return nil, err
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
return nil, fmt.Errorf("http get error : url=%v , statusCode=%v", url, response.StatusCode)
}
return ioutil.ReadAll(response.Body)
}
// Post http post
func Post(url, contentType string, data []byte) ([]byte, error) {
body := bytes.NewBuffer(data)
response, err := http.Post(url, contentType, body)
if err != nil {
return nil, err
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
return nil, fmt.Errorf("http get error : uri=%v , statusCode=%v", url, response.StatusCode)
}
return ioutil.ReadAll(response.Body)
}
// PostWithJSON http post
func PostWithJSON(url string, data []byte) ([]byte, error) {
body := bytes.NewBuffer(data)
response, err := http.Post(url, "application/json", body)
if err != nil {
return nil, err
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
return nil, fmt.Errorf("http post error : uri=%v , statusCode=%v, err=%v", url, response.StatusCode, err)
}
return ioutil.ReadAll(response.Body)
}
// PostWithAuthorizationSerial POST 认证信息 证书编号
func PostWithAuthorizationSerial(url string, data []byte, authorization string, serial string) ([]byte, error) {
body := bytes.NewReader(data)
request, err := http.NewRequest("POST", url, body)
if err != nil {
fmt.Printf("创建HTTP REQUEST错误[%v]", err)
}
request.Header.Set("Content-Type", "application/json")
request.Header.Set("Authorization", authorization)
request.Header.Set("Wechatpay-Serial", serial)
response, err := http.DefaultClient.Do(request)
if err != nil {
return nil, err
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
return nil, fmt.Errorf("http post error : uri=%v , statusCode=%v, err=%v", url, response.StatusCode, err)
}
return ioutil.ReadAll(response.Body)
}