Retry with new authorization token #548
-
I want to refresh my authorization token when it expires and I get 401. So I use I set a new header with |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
I'm trying to define your code like this: package main
import (
"net/http"
"time"
"github.com/go-resty/resty/v2"
)
func main() {
// Create a Resty Client
client := resty.New()
// Retries are configured per client
client = client.
// Set retry count to non zero to enable retries
SetRetryCount(1).
// You can override initial retry wait time.
// Default is 100 milliseconds.
SetRetryWaitTime(5 * time.Second).
// MaxWaitTime can be overridden as well.
// Default is 2 seconds.
SetRetryMaxWaitTime(20 * time.Second).
// SetRetryAfter sets callback to calculate wait time between retries.
// Default (nil) implies exponential backoff with jitter
AddRetryHook(
func(r *resty.Response, err error) {
client = client.SetHeader("Authorization", "auth-2")
},
)
client = client.AddRetryCondition(
// RetryConditionFunc type is for retry condition function
// input: non-nil Response OR request execution error
func(r *resty.Response, err error) bool {
println(r.Request.Header.Get("Authorization"))
println(r.StatusCode())
return r.StatusCode() == http.StatusUnauthorized
},
)
resp, _ := client.R().SetHeader("Authorization", "auth-1").Get("https://webhook.site/9e0d8a64-0ed1-4e28-ae32-ddd26fae5e1d")
println(resp.Request.Header.Get("Authorization"))
} Result:
As you can see, using ResolutionIn this case, you are want to update your header if got 401, right ? You can add do-while function with these step:
I hope this could be help your questions |
Beta Was this translation helpful? Give feedback.
-
The answer is unsatisfying because it requires workarounds to handle exponential back off etc. |
Beta Was this translation helpful? Give feedback.
I'm trying to define your code like this: