From 9f2530d75687305cfb11c4f4cdc0344e0f26a1af Mon Sep 17 00:00:00 2001 From: Alexander Zobnin Date: Fri, 28 Aug 2020 19:11:29 +0300 Subject: [PATCH] Use http.NewRequest() for creating requests, issue #1004 --- pkg/zabbixapi/zabbix_api.go | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/pkg/zabbixapi/zabbix_api.go b/pkg/zabbixapi/zabbix_api.go index ea9cb3f..307d347 100644 --- a/pkg/zabbixapi/zabbix_api.go +++ b/pkg/zabbixapi/zabbix_api.go @@ -6,7 +6,6 @@ import ( "encoding/json" "errors" "fmt" - "io" "io/ioutil" "net/http" "net/url" @@ -108,21 +107,13 @@ func (api *ZabbixAPI) request(ctx context.Context, method string, params ZabbixA return nil, err } - var body io.Reader - body = bytes.NewReader(reqBodyJSON) - rc, ok := body.(io.ReadCloser) - if !ok && body != nil { - rc = ioutil.NopCloser(body) + req, err := http.NewRequest(http.MethodPost, api.url.String(), bytes.NewBuffer(reqBodyJSON)) + if err != nil { + return nil, err } - req := &http.Request{ - Method: "POST", - URL: api.url, - Header: map[string][]string{ - "Content-Type": {"application/json"}, - }, - Body: rc, - } + req.Header.Set("Content-Type", "application/json") + req.Header.Set("User-Agent", "Grafana/grafana-zabbix") response, err := makeHTTPRequest(ctx, api.httpClient, req) if err != nil {