fix body building

master
Paulo Simão 2021-10-09 10:43:06 -03:00
parent 508da7f9cb
commit f0a5dd5025
1 changed files with 8 additions and 9 deletions

17
lib.go
View File

@ -39,15 +39,9 @@ func (c *Cli) Do(method string, strurl string, body []byte) (*http.Response, err
} }
strurl = addr + strurl strurl = addr + strurl
} }
req, err := http.NewRequest(method, strurl, bytes.NewReader(body)) var req *http.Request
if err != nil { var err error
return nil, err
}
if c.headers != nil {
for k, v := range c.headers {
req.Header.Set(k, v)
}
}
if body != nil && len(body) > 0 { if body != nil && len(body) > 0 {
req, err = http.NewRequest(method, strurl, io.NopCloser(bytes.NewReader(body))) 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 { if err != nil {
return nil, err return nil, err
} }
if c.headers != nil {
for k, v := range c.headers {
req.Header.Set(k, v)
}
}
return c.cli.Do(req) return c.cli.Do(req)
} }
func (c *Cli) DoJson(method string, strurl string, i interface{}, o interface{}) (err error) { func (c *Cli) DoJson(method string, strurl string, i interface{}, o interface{}) (err error) {