From ba7ae2ee8ccf545cda8a9869bbe5fa0037a305e0 Mon Sep 17 00:00:00 2001 From: hensur Date: Mon, 9 Apr 2018 10:05:43 +0200 Subject: [PATCH] rest: Add RemoveHeader and SetCookie method These methods extend the rest package to support the cookie header and header deletion. The deletion is necessary to delete an existing authorization header if cookie auth should be used. --- lib/rest/rest.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/lib/rest/rest.go b/lib/rest/rest.go index 3403e4155..847b69310 100644 --- a/lib/rest/rest.go +++ b/lib/rest/rest.go @@ -80,6 +80,14 @@ func (api *Client) SetHeader(key, value string) *Client { return api } +// RemoveHeader unsets a header for all requests +func (api *Client) RemoveHeader(key string) *Client { + api.mu.Lock() + defer api.mu.Unlock() + delete(api.headers, key) + return api +} + // SignerFn is used to sign an outgoing request type SignerFn func(*http.Request) error @@ -100,6 +108,19 @@ func (api *Client) SetUserPass(UserName, Password string) *Client { return api } +// SetCookie creates an Cookies Header for all requests with the supplied +// cookies passed in. +// All cookies have to be supplied at once, all cookies will be overwritten +// on a new call to the method +func (api *Client) SetCookie(cks ...*http.Cookie) *Client { + req, _ := http.NewRequest("GET", "http://example.com", nil) + for _, ck := range cks { + req.AddCookie(ck) + } + api.SetHeader("Cookie", req.Header.Get("Cookie")) + return api +} + // Opts contains parameters for Call, CallJSON etc type Opts struct { Method string // GET, POST etc