http
http copied to clipboard
Update headers on retry
Hi, I'm using the RetryClient() to refresh the access token if expired and then try the request again. However when retrying the request, the old access token is sent and not the new one. This is how I configured my RetryClient:
RetryClient client = RetryClient(
http.Client(),
retries: 1,
when: (response) => response.statusCode == 401,
onRetry: (req, res, retryCount) async{
await authService.refreshToken();
req.headers['Authorization'] = sharedPrefs.accessToken;
}
);
I'm guessing that the req element passed to onRetry can't be used to update the req that is actually sent when retrying. How can I solve this?
Thanks :)
Found the problem. Currently the _onRetry function is a void Function, wheras it should be a Future
This
final void Function(BaseRequest, BaseResponse?, int)? _onRetry;
should be changed to this
final Future<void> Function(BaseRequest, BaseResponse?, int)? _onRetry;
Then at line 121 of RetryClient, _onRetry should be called like this instead:
await Future.delayed(_delay(i));
//_onRetry?.call(request, response, i); THIS DOESN'T SUPPORT FUTURE FUNCTION
if(_onRetry != null) await _onRetry!(request, response, i);
i++;