diff --git a/client.go b/client.go index 145d19b..5ff4f4f 100644 --- a/client.go +++ b/client.go @@ -527,6 +527,16 @@ func (c *Client) HeaderAuthorizationKey() string { return c.headerAuthorizationKey } +// SetHeaderAuthorizationKey method sets the given HTTP header name for Authorization in the client instance. +// +// client.SetHeaderAuthorizationKey("X-Custom-Authorization") +func (c *Client) SetHeaderAuthorizationKey(k string) *Client { + c.lock.Lock() + defer c.lock.Unlock() + c.headerAuthorizationKey = k + return c +} + // SetAuthToken method sets the auth token of the `Authorization` header for all HTTP requests. // The default auth scheme is `Bearer`; it can be customized with the method [Client.SetAuthScheme]. For Example: // @@ -635,6 +645,7 @@ func (c *Client) R() *Request { AllowMethodGetPayload: c.allowMethodGetPayload, AllowMethodDeletePayload: c.allowMethodDeletePayload, AllowNonIdempotentRetry: c.allowNonIdempotentRetry, + HeaderAuthorizationKey: c.headerAuthorizationKey, client: c, baseURL: c.baseURL, diff --git a/client_test.go b/client_test.go index 7b33f2a..258c374 100644 --- a/client_test.go +++ b/client_test.go @@ -524,6 +524,10 @@ func TestClientSettingsCoverage(t *testing.T) { c.SetAuthToken(authToken) assertEqual(t, authToken, c.AuthToken()) + customAuthHeader := "X-Custom-Authorization" + c.SetHeaderAuthorizationKey(customAuthHeader) + assertEqual(t, customAuthHeader, c.HeaderAuthorizationKey()) + c.SetCloseConnection(true) c.DisableDebug() diff --git a/middleware.go b/middleware.go index 3f5e5cf..afb7dd8 100644 --- a/middleware.go +++ b/middleware.go @@ -284,7 +284,7 @@ func addCredentials(c *Client, r *Request) error { // Build the token Auth header if !isStringEmpty(r.AuthToken) { credentialsAdded = true - r.RawRequest.Header.Set(c.HeaderAuthorizationKey(), r.AuthScheme+" "+r.AuthToken) + r.RawRequest.Header.Set(r.HeaderAuthorizationKey, r.AuthScheme+" "+r.AuthToken) } if !c.IsDisableWarn() && credentialsAdded { diff --git a/request.go b/request.go index 5556752..155d9a3 100644 --- a/request.go +++ b/request.go @@ -62,6 +62,7 @@ type Request struct { IsDone bool IsSaveResponse bool Timeout time.Duration + HeaderAuthorizationKey string RetryCount int RetryWaitTime time.Duration RetryMaxWaitTime time.Duration