diff --git a/client.go b/client.go index 61e158ff..e2f34ed4 100644 --- a/client.go +++ b/client.go @@ -91,6 +91,7 @@ type Client struct { scheme string proxyURL *url.URL mutex *sync.Mutex + closeConnection bool beforeRequest []func(*Client, *Request) error afterResponse []func(*Client, *Response) error } @@ -595,6 +596,13 @@ func (c *Client) SetScheme(scheme string) *Client { return c } +// SetCloseConnection method sets variable Close in http request struct with the given +// value. More info: https://golang.org/src/net/http/request.go +func (c *Client) SetCloseConnection(close bool) *Client { + c.closeConnection = close + return c +} + // executes the given `Request` object and returns response func (c *Client) execute(req *Request) (*Response, error) { // Apply Request middleware diff --git a/default.go b/default.go index fb02a2b1..40b6ad19 100644 --- a/default.go +++ b/default.go @@ -222,6 +222,12 @@ func SetScheme(scheme string) *Client { return DefaultClient.SetScheme(scheme) } +// SetCloseConnection method sets close connection value in the resty client. +// See `Client.SetCloseConnection` for more information. +func SetCloseConnection(close bool) *Client { + return DefaultClient.SetCloseConnection(close) +} + func init() { DefaultClient = New() } diff --git a/middleware.go b/middleware.go index 7968bf7b..83d1435d 100644 --- a/middleware.go +++ b/middleware.go @@ -139,6 +139,10 @@ func createHTTPRequest(c *Client, r *Request) (err error) { r.RawRequest, err = http.NewRequest(r.Method, r.URL, r.bodyBuf) } + if err == nil { + r.RawRequest.Close = c.closeConnection + } + // Add headers into http request r.RawRequest.Header = r.Header diff --git a/resty_test.go b/resty_test.go index 4461b0e0..7db48ac7 100644 --- a/resty_test.go +++ b/resty_test.go @@ -1350,6 +1350,9 @@ func TestClientOptions(t *testing.T) { SetScheme("http") assertEqual(t, DefaultClient.scheme, "http") + SetCloseConnection(true) + assertEqual(t, DefaultClient.closeConnection, true) + SetLogger(ioutil.Discard) }