Skip to content

Commit ff2bc05

Browse files
author
bajins
committed
add
1 parent 82c45b6 commit ff2bc05

File tree

2 files changed

+58
-10
lines changed

2 files changed

+58
-10
lines changed

utils/http.go

Lines changed: 57 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,19 @@ func HttpRequest(method, urlText, contentType string, params, header map[string]
190190
return client.Do(req)
191191
}
192192

193+
// 请求并读取返回内容
194+
func HttpReadBody(method, urlText, contentType string, params, header map[string]string) ([]byte, error) {
195+
res, err := HttpRequest(method, urlText, contentType, params, header)
196+
if err != nil {
197+
return nil, err
198+
}
199+
result, err := ioutil.ReadAll(res.Body)
200+
if err != nil {
201+
return nil, err
202+
}
203+
return result, nil
204+
}
205+
193206
// 请求并读取返回内容为字符串
194207
func HttpReadBodyString(method, urlText, contentType string, params, header map[string]string) (string, error) {
195208
res, err := HttpRequest(method, urlText, contentType, params, header)
@@ -203,18 +216,41 @@ func HttpReadBodyString(method, urlText, contentType string, params, header map[
203216
return string(result), nil
204217
}
205218

206-
// 请求并读取返回内容为json
207-
func HttpReadBodyJson(method, urlText, contentType string, params, header map[string]string) (map[string]interface{}, error) {
208-
res, err := HttpRequest(method, urlText, contentType, params, header)
219+
// 请求并读取返回内容为json对象
220+
func HttpReadBodyJsonObject(method, urlText, contentType string, params, header map[string]string, obj *interface{}) error {
221+
res, err := HttpReadBody(method, urlText, contentType, params, header)
209222
if err != nil {
210-
return nil, err
223+
return err
211224
}
212-
result, err := ioutil.ReadAll(res.Body)
225+
err = json.Unmarshal(res, obj)
226+
if err != nil {
227+
return err
228+
}
229+
return nil
230+
}
231+
232+
// 请求并读取返回内容为json map
233+
func HttpReadBodyJsonMap(method, urlText, contentType string, params, header map[string]string) (map[string]interface{}, error) {
234+
res, err := HttpReadBody(method, urlText, contentType, params, header)
213235
if err != nil {
214236
return nil, err
215237
}
216238
var data map[string]interface{}
217-
err = json.Unmarshal(result, &data)
239+
err = json.Unmarshal(res, &data)
240+
if err != nil {
241+
return nil, err
242+
}
243+
return data, nil
244+
}
245+
246+
// 请求并读取返回内容为json数组
247+
func HttpReadBodyJsonArray(method, urlText, contentType string, params, header map[string]string) ([]map[string]interface{}, error) {
248+
res, err := HttpReadBody(method, urlText, contentType, params, header)
249+
if err != nil {
250+
return nil, err
251+
}
252+
var data []map[string]interface{}
253+
err = json.Unmarshal(res, &data)
218254
if err != nil {
219255
return nil, err
220256
}
@@ -229,14 +265,26 @@ type HttpClient struct {
229265
Header map[string]string
230266
}
231267

232-
func (hc *HttpClient) ReadBody() (*http.Response, error) {
268+
func (hc *HttpClient) HttpRequest() (*http.Response, error) {
233269
return HttpRequest(hc.Method, hc.UrlText, hc.ContentType, hc.Params, hc.Header)
234270
}
235271

272+
func (hc *HttpClient) ReadBody() ([]byte, error) {
273+
return HttpReadBody(hc.Method, hc.UrlText, hc.ContentType, hc.Params, hc.Header)
274+
}
275+
236276
func (hc *HttpClient) ReadBodyString() (string, error) {
237277
return HttpReadBodyString(hc.Method, hc.UrlText, hc.ContentType, hc.Params, hc.Header)
238278
}
239279

240-
func (hc *HttpClient) ReadBodyJson() (map[string]interface{}, error) {
241-
return HttpReadBodyJson(hc.Method, hc.UrlText, hc.ContentType, hc.Params, hc.Header)
280+
func (hc *HttpClient) HttpReadBodyJsonObject(obj *interface{}) error {
281+
return HttpReadBodyJsonObject(hc.Method, hc.UrlText, hc.ContentType, hc.Params, hc.Header, obj)
282+
}
283+
284+
func (hc *HttpClient) HttpReadBodyJsonMap() (map[string]interface{}, error) {
285+
return HttpReadBodyJsonMap(hc.Method, hc.UrlText, hc.ContentType, hc.Params, hc.Header)
286+
}
287+
288+
func (hc *HttpClient) ReadBodyJsonArray() ([]map[string]interface{}, error) {
289+
return HttpReadBodyJsonArray(hc.Method, hc.UrlText, hc.ContentType, hc.Params, hc.Header)
242290
}

utils/utils_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func TestHttp(t *testing.T) {
9090
Params: nil,
9191
Header: nil,
9292
}
93-
t.Log(httpClient.ReadBodyJson())
93+
t.Log(httpClient.HttpReadBodyJsonMap())
9494
}
9595

9696
func TestSchedulerIntervalsTimer(t *testing.T) {

0 commit comments

Comments
 (0)