Search code examples
swiftauthenticationnetwork-programmingalamofire

Is there an another way to call retry funcation except response code equal 400- 500 when i using RequestInterceptor of Alamofire


I want to refresh the token when it expires, but the server returns 200 not 401 so the retry method will not execute.

Is there a way to manually call retry? Or some other solution?


Solution

  • You can customize Alamofire's .validate closure to produce an error when certain responses have a 200 status code.

    .validate { request, response, data in
      guard response is token expiration else { return .success(()) }
    
      return .failure(.responseValidationFailed(.customValidationFailed(error: <your error>)))
    }
    

    The error your produce here will be available to the retriers later in the pipeline so you can detect token expiration and issue the refresh.