From 09cc8179cc235df449e07c6a42edd76210a2b759 Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Mon, 4 Mar 2024 15:23:19 +0000 Subject: [PATCH] lib/rest: add CheckRedirect function for redirect management --- lib/rest/rest.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/rest/rest.go b/lib/rest/rest.go index a7bba472b..61522577d 100644 --- a/lib/rest/rest.go +++ b/lib/rest/rest.go @@ -149,6 +149,8 @@ type Opts struct { Trailer *http.Header // set the request trailer Close bool // set to close the connection after this transaction NoRedirect bool // if this is set then the client won't follow redirects + // On Redirects, call this function - see the http.Client docs: https://pkg.go.dev/net/http#Client + CheckRedirect func(req *http.Request, via []*http.Request) error } // Copy creates a copy of the options @@ -299,6 +301,10 @@ func (api *Client) Call(ctx context.Context, opts *Opts) (resp *http.Response, e var c *http.Client if opts.NoRedirect { c = ClientWithNoRedirects(api.c) + } else if opts.CheckRedirect != nil { + clientCopy := *api.c + clientCopy.CheckRedirect = opts.CheckRedirect + c = &clientCopy } else { c = api.c }