Retrying failed requests
By default, the Ktor client doesn't retry requests that failed due to network or server errors. You can use the HttpRequestRetry plugin to configure the retry policy for failed requests in various ways: specify the number of retries, configure conditions for retrying a request, or modify a request before retrying.
Add dependencies
HttpRequestRetry
only requires the ktor-client-core artifact and doesn't need any specific dependencies.
Install HttpRequestRetry
To install HttpRequestRetry
, pass it to the install
function inside a client configuration block:
Configure HttpRequestRetry
Basic retry configuration
A runnable example below shows how to configure the basic retry policy:
The
retryOnServerErrors
function enables retrying a request if a5xx
response is received from a server and specifies the number of retries.exponentialDelay
specifies an exponential delay between retries, which is calculated using the Exponential backoff algorithm.
You can learn more about supported configuration options from HttpRequestRetryConfig.
Configure retry conditions
There are also configuration settings that allow you to configure conditions for retrying a request or specify delay logic:
Modify a request before retrying
If you need to modify a request before retrying, use modifyRequest
: