From f0a5dd5025ea94ce7d5ada99a7ae0e9682d122bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paulo=20Sima=CC=83o?= Date: Sat, 9 Oct 2021 10:43:06 -0300 Subject: [PATCH] fix body building --- lib.go | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/lib.go b/lib.go index f91e421..c9a6811 100644 --- a/lib.go +++ b/lib.go @@ -39,15 +39,9 @@ func (c *Cli) Do(method string, strurl string, body []byte) (*http.Response, err } strurl = addr + strurl } - req, err := http.NewRequest(method, strurl, bytes.NewReader(body)) - if err != nil { - return nil, err - } - if c.headers != nil { - for k, v := range c.headers { - req.Header.Set(k, v) - } - } + var req *http.Request + var err error + if body != nil && len(body) > 0 { req, err = http.NewRequest(method, strurl, io.NopCloser(bytes.NewReader(body))) @@ -57,6 +51,11 @@ func (c *Cli) Do(method string, strurl string, body []byte) (*http.Response, err if err != nil { return nil, err } + if c.headers != nil { + for k, v := range c.headers { + req.Header.Set(k, v) + } + } return c.cli.Do(req) } func (c *Cli) DoJson(method string, strurl string, i interface{}, o interface{}) (err error) {