From 43922b2afb1f5b0bac0b1cc0f16cf38957b5a831 Mon Sep 17 00:00:00 2001 From: kenneth <1185230223@qq.com> Date: Wed, 13 Apr 2022 09:54:36 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0GET=E8=AF=B7=E6=B1=82?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Get请求方法,带json的body --- pkg/fetcher/fetcher.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/pkg/fetcher/fetcher.go b/pkg/fetcher/fetcher.go index 44a13c0..81e7661 100644 --- a/pkg/fetcher/fetcher.go +++ b/pkg/fetcher/fetcher.go @@ -34,6 +34,29 @@ func Get(url string) ([]byte, error) { return res, nil } +// GetJson application/json get 请求 +func GetJson(url string, parameter []byte, timeout int) ([]byte, error) { + client := &http.Client{Timeout: time.Second * time.Duration(timeout)} + byteParameter := bytes.NewBuffer(parameter) + req, err := http.NewRequest("GET", url, byteParameter) + if err != nil { + return nil, err + } + + resp, err := client.Do(req) + if err != nil { + return nil, err + } + defer resp.Body.Close() + + res, err := ioutil.ReadAll(resp.Body) + if err != nil { + return nil, err + } + + return res, nil +} + // PostJson application/json post 请求 func PostJson(url, parameter string, timeout int) ([]byte, error) { client := &http.Client{Timeout: time.Second * time.Duration(timeout)}