1

How can I modify an AFHTTPRequestOperation header after creating it? In situations where an API request returns a 401, i need to refresh the access token and adjust every AFHTTPRequestOperation's header, and retry the same request operation with the updated access token.

Below is my code to process requests that were queued due to an access token is currently being refreshed. When a new access token is received, this method is called.

The method below works, but it just seems odd that creating a new variable of the operation request and adjusting the variable's header also changes the original request.

var authManager = AFOAuth2Manager()

func processHeldRequests() {
        for operation: AFHTTPRequestOperation in heldRequests {
            var token = tokenManager.getToken()
            println("adjusting held operation's request header authorization to new token \(token!)")

            var operationRequest: NSMutableURLRequest = operation.request as! NSMutableURLRequest
            operationRequest.setValue("Bearer \(token!)", forHTTPHeaderField: "Authorization")
            authManager.operationQueue.addOperation(operation)
        }
        heldRequests.removeAll(keepCapacity: false)
    }
3254523
  • 3,016
  • 7
  • 29
  • 43

0 Answers0