-
Notifications
You must be signed in to change notification settings - Fork 4
/
http.go
85 lines (77 loc) · 2.3 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
package goutils
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"mime/multipart"
"net/http"
"net/url"
"github.com/pkg/errors"
)
// 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 请求
func HTTPPOST(ctx context.Context, cli *http.Client, req *http.Request, rspPointer interface{}) error {
resp, err := cli.Do(req)
if err != nil {
return errors.Wrap(err, "POST do request error")
}
defer resp.Body.Close()
rspbuf, err := ioutil.ReadAll(resp.Body)
if err != nil {
return errors.Wrap(err, fmt.Sprint("POST read resp body error, resp.Body:", resp.Body))
}
if err := json.Unmarshal(rspbuf, rspPointer); err != nil {
return errors.Wrap(err, "POST json unmarshal result error")
}
return 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 请求
func HTTPGET(ctx context.Context, cli *http.Client, apiurl string, rspPointer interface{}) error {
resp, err := cli.Get(apiurl)
if err != nil {
return errors.Wrap(err, "Get request error")
}
defer resp.Body.Close()
rspbuf, err := ioutil.ReadAll(resp.Body)
if err != nil {
return errors.Wrap(err, fmt.Sprint("GET read resp body error, resp.Body:", resp.Body))
}
if err := json.Unmarshal(rspbuf, rspPointer); err != nil {
return errors.Wrap(err, "GET json unmarshal result error")
}
return nil
}