14

I am using the RequestRetrier of Alamofire 4.0 to control the retrying of requests for expired access token. I am following the documentation here.

I have a very similar implementation to the example available in the documentation OAuth2Handler which implements RequestAdapter and RequestRetrier.

The issue I am encountering is that func should(_ manager: SessionManager, retry request: Request, with error: Error, completion: @escaping RequestRetryCompletion) is never being called. The adapt method of the RequestAdapter implementation does get called though.

Debugging, I see that SessionDelegate only calls should(_,retry,with,completion) when there is an error, but requests that return status codes related to Authorization issues, don't seem to generate errors, so that method never gets called.

Am I missing something here?

2 Answers 2

25

Maybe you are not getting an error. 400 responses aren't considered as error by Alamofire. In case you want get an error when receiving a 400 code you should chain validate() to the request. If this is your case you can find more information here.

1
  • 1
    Forgot to call validate! That was it, thanks @crisisGriega! Commented Sep 20, 2016 at 15:45
13

Following the example in the documentation exactly , mine didn't work.I was already using validate() as shown in the example.

let sessionManager = SessionManager()
sessionManager.adapter = oauthHandler
sessionManager.retrier = oauthHandler
let urlString = "\(baseURLString)/some/endpoint"

sessionManager.request(urlString).validate().responseJSON { response in
debugPrint(response)
}

Although , after replacing SessionManager() with Alamofire.SessionManager.default , the method func should(_ manager: SessionManager, retry request: Request, with error: Error, completion: @escaping RequestRetryCompletion) gets invoked.

let sessionManager = Alamofire.SessionManager.default
4
  • Have you figured out why this is happening, I'm having the same issue
    – Andrea
    Commented Nov 27, 2017 at 4:30
  • @Andrea No I haven't.
    – Ashildr
    Commented Nov 28, 2017 at 2:14
  • Needed that too, to make it work: let sessionManager = Alamofire.SessionManager.default Commented Jan 28, 2018 at 13:25
  • 1
    try sessionManager.validate(statusCodes: [200])
    – anders
    Commented Mar 19, 2018 at 13:45

Not the answer you're looking for? Browse other questions tagged or ask your own question.