-
Notifications
You must be signed in to change notification settings - Fork 7
/
httpapi.go
135 lines (124 loc) · 3.54 KB
/
httpapi.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package httpapi
import (
"bytes"
"io/ioutil"
"net/http"
"time"
)
func PostJson(url string, jsonStr []byte, HeaderData map[string]string) (bool, string) {
req, _ := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
if HeaderData != nil {
for headerkey, headervalue := range HeaderData {
// req.Header.Set("Content-Type", "application/json")
req.Header.Set(headerkey, headervalue)
}
}
client := &http.Client{Timeout: 10000 * time.Millisecond}
resp, err := client.Do(req)
if err != nil {
errinfo := "Post请求失败:" + err.Error()
return false, errinfo
}
if resp.StatusCode != 200 {
errinfo := "Get请求失败,状态码:" + resp.Status
return false, errinfo
}
defer resp.Body.Close()
// statuscode := resp.StatusCode
// hea := resp.Header
body, _ := ioutil.ReadAll(resp.Body)
return true, string(body)
}
func GetDefault(url string, UriData map[string]string, HeaderData map[string]string) (bool, string) {
req, _ := http.NewRequest("GET", url, nil)
if HeaderData != nil {
for headerkey, headervalue := range HeaderData {
// req.Header.Set("Content-Type", "application/json")
req.Header.Set(headerkey, headervalue)
}
}
if UriData != nil {
q := req.URL.Query()
for urikey, urivalue := range UriData {
q.Add(urikey, urivalue)
}
req.URL.RawQuery = q.Encode()
}
client := &http.Client{Timeout: 10000 * time.Millisecond}
resp, err := client.Do(req)
if err != nil {
errinfo := "Get请求失败" + err.Error()
return false, errinfo
}
if resp.StatusCode != 200 {
errinfo := "Get请求失败,状态码:" + resp.Status
return false, errinfo
}
defer resp.Body.Close()
// statuscode := resp.StatusCode
// hea := resp.Header
body, _ := ioutil.ReadAll(resp.Body)
return true, string(body)
}
func PutDefault(url string, UriData map[string]string, HeaderData map[string]string) (bool, string) {
req, _ := http.NewRequest("PUT", url, nil)
if HeaderData != nil {
for headerkey, headervalue := range HeaderData {
// req.Header.Set("Content-Type", "application/json")
req.Header.Set(headerkey, headervalue)
}
}
if UriData != nil {
q := req.URL.Query()
for urikey, urivalue := range UriData {
q.Add(urikey, urivalue)
}
req.URL.RawQuery = q.Encode()
}
client := &http.Client{Timeout: 10000 * time.Millisecond}
resp, err := client.Do(req)
if err != nil {
errinfo := "Put请求失败" + err.Error()
return false, errinfo
}
if resp.StatusCode != 200 {
errinfo := "Put请求失败,状态码:" + resp.Status
return false, errinfo
}
defer resp.Body.Close()
// statuscode := resp.StatusCode
// hea := resp.Header
body, _ := ioutil.ReadAll(resp.Body)
return true, string(body)
}
func DeleteDefault(url string, UriData map[string]string, HeaderData map[string]string) (bool, string) {
req, _ := http.NewRequest("DELETE", url, nil)
if HeaderData != nil {
for headerkey, headervalue := range HeaderData {
// req.Header.Set("Content-Type", "application/json")
req.Header.Set(headerkey, headervalue)
}
}
if UriData != nil {
q := req.URL.Query()
for urikey, urivalue := range UriData {
q.Add(urikey, urivalue)
}
req.URL.RawQuery = q.Encode()
}
client := &http.Client{Timeout: 10000 * time.Millisecond}
resp, err := client.Do(req)
if err != nil {
errinfo := "DELETE请求失败" + err.Error()
return false, errinfo
}
if resp.StatusCode != 200 {
errinfo := "DELETE请求失败,状态码:" + resp.Status
return false, errinfo
}
defer resp.Body.Close()
// statuscode := resp.StatusCode
// hea := resp.Header
body, _ := ioutil.ReadAll(resp.Body)
return true, string(body)
}