forked from YaoApp/yao
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest.go
116 lines (104 loc) · 2.9 KB
/
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
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
package helper
import (
"bytes"
"io/ioutil"
"net/http"
"strings"
jsoniter "github.com/json-iterator/go"
)
// Response 请求响应结果
type Response struct {
Status int `json:"status"`
Body string `json:"body"`
Data interface{} `json:"data"`
Headers map[string]interface{} `json:"headers"`
}
// RequestGet 发送GET请求
func RequestGet(url string, params map[string]interface{}, headers map[string]string) Response {
return RequestSend("GET", url, params, nil, headers)
}
// RequestPost 发送POST请求
func RequestPost(url string, data interface{}, headers map[string]string) Response {
return RequestSend("POST", url, map[string]interface{}{}, data, headers)
}
// RequestSend 发送Request请求
func RequestSend(method string, url string, params map[string]interface{}, data interface{}, headers map[string]string) Response {
var body []byte
var err error
if data != nil {
body, err = jsoniter.Marshal(data)
if err != nil {
return Response{
Status: 500,
Body: err.Error(),
Data: map[string]interface{}{"code": 500, "message": err.Error()},
Headers: map[string]interface{}{
"Content-Type": "application/json;charset=utf8",
},
}
}
}
req, err := http.NewRequest(method, url, bytes.NewBuffer(body))
if err != nil {
return Response{
Status: 500,
Body: err.Error(),
Data: map[string]interface{}{"code": 500, "message": err.Error()},
Headers: map[string]interface{}{
"Content-Type": "application/json;charset=utf8",
},
}
}
// Request Header
for name, header := range headers {
req.Header.Set(name, header)
}
resp, err := (&http.Client{}).Do(req)
if err != nil {
return Response{
Status: 0,
Body: err.Error(),
Data: map[string]interface{}{"code": 500, "message": err.Error()},
Headers: map[string]interface{}{
"Content-Type": "application/json;charset=utf8",
},
}
}
defer resp.Body.Close()
body, err = ioutil.ReadAll(resp.Body) // response body is []byte
if err != nil {
return Response{
Status: 500,
Body: err.Error(),
Data: map[string]interface{}{"code": resp.StatusCode, "message": err.Error()},
Headers: map[string]interface{}{
"Content-Type": "application/json;charset=utf8",
},
}
}
// JSON 解析
var res interface{}
if strings.HasPrefix(resp.Header.Get("Content-Type"), "application/json") {
err = jsoniter.Unmarshal(body, &res)
if err != nil {
return Response{
Status: 500,
Body: err.Error(),
Data: map[string]interface{}{"code": resp.StatusCode, "message": err.Error()},
Headers: map[string]interface{}{
"Content-Type": "application/json;charset=utf8",
},
}
}
}
respHeaders := map[string]interface{}{}
for name := range resp.Header {
respHeaders[name] = resp.Header.Get(name)
}
return Response{
Status: resp.StatusCode,
Body: string(body),
Data: res,
Headers: respHeaders,
}
}