From 8894987d0bef540b0ebe2099cca05db1df4c6623 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 11 Mar 2015 20:02:11 +0100 Subject: [PATCH] api: document jar requirement --- api/client.go | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/api/client.go b/api/client.go index 3afe7d6709..814e3edc56 100644 --- a/api/client.go +++ b/api/client.go @@ -2,6 +2,7 @@ package api import ( "net/http" + "net/http/cookiejar" "net/url" ) @@ -14,7 +15,10 @@ type Config struct { Address string // HttpClient is the HTTP client to use. http.DefaultClient will be - // used if not specified. + // used if not specified. The HTTP client must have the cookie jar set + // to be able to store cookies, otherwise authentication (login) will + // not work properly. If the jar is nil, a default empty cookie jar + // will be set. HttpClient *http.Client } @@ -43,6 +47,21 @@ func NewClient(c Config) (*Client, error) { return nil, err } + // Make a copy of the HTTP client so we can configure it without + // affecting the original + // + // If no cookie jar is set on the client, we set a default empty + // cookie jar. + client := *c.HttpClient + if client.Jar == nil { + jar, err := cookiejar.New(&cookiejar.Options{}) + if err != nil { + return nil, err + } + + client.Jar = jar + } + return &Client{ addr: u, config: c,