diff --git a/client.go b/client.go index 32385f95..9cb934f3 100644 --- a/client.go +++ b/client.go @@ -150,6 +150,17 @@ func (c *Client) SetHeaders(headers map[string]string) *Client { return c } +// SetCookieJar method sets custom http.CookieJar in the resty client. Its way to override default. +// Example: sometimes we don't want to save cookies in api contacting, we can remove the default +// CookieJar in resty client. +// +// resty.SetCookieJar(nil) +// +func (c *Client) SetCookieJar(jar http.CookieJar) *Client { + c.httpClient.Jar = jar + return c +} + // SetCookie method sets a single cookie in the client instance. // These cookies will be added to all the request raised from this client instance. // resty.SetCookie(&http.Cookie{ diff --git a/client_test.go b/client_test.go index 50d57618..64897c99 100644 --- a/client_test.go +++ b/client_test.go @@ -220,6 +220,17 @@ func TestSetScheme(t *testing.T) { assertEqual(t, true, DefaultClient.scheme == "http") } +func TestSetCookieJar(t *testing.T) { + DefaultClient = dc() + backupJar := DefaultClient.httpClient.Jar + + SetCookieJar(nil) + assertEqual(t, true, DefaultClient.httpClient.Jar == nil) + + SetCookieJar(backupJar) + assertEqual(t, true, DefaultClient.httpClient.Jar == backupJar) +} + func TestClientOptions(t *testing.T) { SetHTTPMode().SetContentLength(true) assertEqual(t, Mode(), "http") diff --git a/default.go b/default.go index c6c43e86..31c605ad 100644 --- a/default.go +++ b/default.go @@ -89,6 +89,11 @@ func SetHeaders(headers map[string]string) *Client { return DefaultClient.SetHeaders(headers) } +// SetCookieJar sets custom http.CookieJar. See `Client.SetCookieJar` for more information. +func SetCookieJar(jar http.CookieJar) *Client { + return DefaultClient.SetCookieJar(jar) +} + // SetCookie sets single cookie object. See `Client.SetCookie` for more information. func SetCookie(hc *http.Cookie) *Client { return DefaultClient.SetCookie(hc)