fix crash when req is called with no body

This commit is contained in:
Ringo Hoffmann 2021-11-08 23:10:07 +01:00 committed by Christoph Polcin
parent adba8dc051
commit 3f8721cd4b

View File

@ -13,6 +13,7 @@ func (c *Client) req(method, path string, body io.Reader, intercept func(*http.R
var r *http.Request
var retryBuf io.Reader
if body != nil {
// If the authorization fails, we will need to restart reading
// from the passed body stream.
// When body is seekable, use seek to reset the streams
@ -29,11 +30,9 @@ func (c *Client) req(method, path string, body io.Reader, intercept func(*http.R
retryBuf = buff
body = io.TeeReader(body, buff)
}
if body == nil {
r, err = http.NewRequest(method, PathEscape(Join(c.root, path)), nil)
} else {
r, err = http.NewRequest(method, PathEscape(Join(c.root, path)), body)
} else {
r, err = http.NewRequest(method, PathEscape(Join(c.root, path)), nil)
}
if err != nil {
@ -82,12 +81,9 @@ func (c *Client) req(method, path string, body io.Reader, intercept func(*http.R
return rs, newPathError("Authorize", c.root, rs.StatusCode)
}
if body == nil {
return c.req(method, path, nil, intercept)
} else {
// retryBuf will be nil if body was nil initially so no check
// for body == nil is required here.
return c.req(method, path, retryBuf, intercept)
}
} else if rs.StatusCode == 401 {
return rs, newPathError("Authorize", c.root, rs.StatusCode)
}