-
Notifications
You must be signed in to change notification settings - Fork 4
/
http.go
117 lines (106 loc) · 3.43 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
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
package goutils
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"mime/multipart"
"net/http"
"net/url"
"github.com/pkg/errors"
)
// NewHTTPJSONReq 根据参数创建 json 请求
func NewHTTPJSONReq(ctx context.Context, apiurl string, reqData interface{}) (*http.Request, error) {
reqbuf, err := json.Marshal(reqData)
if err != nil {
return nil, errors.Wrap(err, fmt.Sprint("json marshal error"))
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, apiurl, bytes.NewReader(reqbuf))
if err != nil {
return nil, errors.Wrap(err, "Post NewRequestWithContext")
}
req.Header.Set("Content-Type", "application/json")
return req, nil
}
// NewHTTPMultipartReq 根据参数创建 form-data 请求
func NewHTTPMultipartReq(ctx context.Context, apiurl string, reqData map[string]string) (*http.Request, error) {
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
for k, v := range reqData {
if err := writer.WriteField(k, v); err != nil {
return nil, errors.Wrap(err, "WriteField error")
}
}
if err := writer.Close(); err != nil {
return nil, errors.Wrap(err, "Writer close error")
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, apiurl, body)
if err != nil {
return nil, errors.Wrap(err, "NewRequestWithContext error")
}
req.Header.Set("Content-Type", writer.FormDataContentType())
return req, nil
}
// HTTPPOST 发送 http post 请求并将结果 json unmarshal 到 rspPointer
func HTTPPOST(ctx context.Context, cli *http.Client, req *http.Request, rspPointer interface{}) error {
rspbuf, err := HTTPPOSTRaw(ctx, cli, req)
if err != nil {
return err
}
if err := json.Unmarshal(rspbuf, rspPointer); err != nil {
return errors.Wrap(err, fmt.Sprintf("json unmarshal result error, rspbuf:%s", string(rspbuf)))
}
return nil
}
// HTTPPOSTRaw 发送 http post 请求
func HTTPPOSTRaw(ctx context.Context, cli *http.Client, req *http.Request) ([]byte, error) {
resp, err := cli.Do(req)
if err != nil {
return nil, errors.Wrap(err, "POST do request error")
}
defer resp.Body.Close()
rspbuf, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, errors.Wrap(err, fmt.Sprint("read resp body error, resp.Body:", resp.Body))
}
return rspbuf, nil
}
// NewHTTPGetURLWithQueryString 创建带 querystring 的 http get 请求 url
func NewHTTPGetURLWithQueryString(ctx context.Context, apiurl string, params map[string]string) (string, error) {
value := url.Values{}
u, err := url.Parse(apiurl)
if err != nil {
return "", err
}
for k, v := range params {
value.Set(k, v)
}
u.RawQuery = value.Encode()
apiurl = u.String()
return apiurl, nil
}
// HTTPGET 发送 http get 请求并将返回结果 json unmarshal 到 rspPointer
func HTTPGET(ctx context.Context, cli *http.Client, apiurl string, rspPointer interface{}) error {
rspbuf, err := HTTPGETRaw(ctx, cli, apiurl)
if err != nil {
return err
}
if err := json.Unmarshal(rspbuf, rspPointer); err != nil {
return errors.Wrap(err, fmt.Sprintf("json unmarshal result error, rspbuf:%s", string(rspbuf)))
}
return nil
}
// HTTPGETRaw 发送 http get 请求
func HTTPGETRaw(ctx context.Context, cli *http.Client, apiurl string) ([]byte, error) {
resp, err := cli.Get(apiurl)
if err != nil {
return nil, errors.Wrap(err, "Get request error")
}
defer resp.Body.Close()
rspbuf, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, errors.Wrap(err, fmt.Sprint("read resp body error, resp.Body:", resp.Body))
}
return rspbuf, nil
}