Search code examples
c#.netrest-clientsession-managementflurl

Flurl: Retry http call automatically after handling 401 response


Using Flurl, I am trying to implement an elegant solution to handle 401 responses for several APIs.

I can trap HTTP exceptions using:

FlurlHttp.GlobalSettings.OnError = MyFlurlErrorHandler;

And then:

private void MyFlurlErrorHandler(HttpCall httpCall)
{
    if (httpCall.HttpStatus == System.Net.HttpStatusCode.Unauthorized)       //401
    {
         //some code here will refresh our access token to take care of the 401 error

         //SESSION MANAGEMENT HERE
         httpCall.ExceptionHandled = true
    }
}

Once the error is handled, how do I automatically retry the original httpCall here before setting ExceptionHandled = true?


Solution

  • Flurl has nothing built-in to do retries, although it is on the roadmap. Until then I recommend using Polly.