Request

public class Request

Request is the common superclass of all Alamofire request types and provides common state, delegate, and callback handling.

  • State of the Request, with managed transitions between states set when calling resume(), suspend(), or cancel() on the Request.

    • initialized: Initial state of the Request.
    • resumed: Set when resume() is called. Any tasks created for the Request will have resume() called on them in this state.
    • suspended: Set when suspend() is called. Any tasks created for the Request will have suspend() called on them in this state.
    • cancelled: Set when cancel() is called. Any tasks created for the Request will have cancel() called on them. Unlike resumed or suspended, once in the cancelled state, the Request can no longer transition to any other state.
    See more

    Declaration

    Swift

    public enum State
  • id

    UUID prividing a unique identifier for the Request, used in the Hashable and Equatable conformances.

    Declaration

    Swift

    public let id: UUID
  • The serial queue for all internal async actions.

    Declaration

    Swift

    public let underlyingQueue: DispatchQueue
  • The queue used for all serialization actions. By default it’s a serial queue that targets underlyingQueue.

    Declaration

    Swift

    public let serializationQueue: DispatchQueue
  • EventMonitor used for event callbacks.

    Declaration

    Swift

    public let eventMonitor: EventMonitor?
  • The Request‘s interceptor.

    Declaration

    Swift

    public let interceptor: RequestInterceptor?
  • The Request‘s delegate.

    Declaration

    Swift

    public private(set) weak var delegate: RequestDelegate? {
      get
      }
  • State of the Request.

    Declaration

    Swift

    public fileprivate(set) var state: State { get set }
  • Returns whether state is .cancelled.

    Declaration

    Swift

    public var isCancelled: Bool { get }
  • Returns whether state is.resumed`.

    Declaration

    Swift

    public var isResumed: Bool { get }
  • Returns whether state is .suspended.

    Declaration

    Swift

    public var isSuspended: Bool { get }
  • Returns whether state is .initialized.

    Declaration

    Swift

    public var isInitialized: Bool { get }
  • Closure type executed when monitoring the upload or download progress of a request.

    Declaration

    Swift

    public typealias ProgressHandler = (Progress) -> Void
  • Progress of the upload of the body of the executed URLRequest. Reset to 0 if the Request is retried.

    Declaration

    Swift

    public let uploadProgress: Progress
  • Progress of the download of any response data. Reset to 0 if the Request is retried.

    Declaration

    Swift

    public let downloadProgress: Progress
  • Undocumented

    Declaration

    Swift

    public private(set) var redirectHandler: RedirectHandler? { get set }
  • Undocumented

    Declaration

    Swift

    public private(set) var cachedResponseHandler: CachedResponseHandler? { get set }
  • URLCredential used for authentication challenges. Created by calling one of the authenticate methods.

    Declaration

    Swift

    public private(set) var credential: URLCredential? { get set }
  • All URLRequests created on behalf of the Request, including original and adapted requests.

    Declaration

    Swift

    public var requests: [URLRequest] { get }
  • First URLRequest created on behalf of the Request. May not be the first one actually executed.

    Declaration

    Swift

    public var firstRequest: URLRequest? { get }
  • Last URLRequest created on behalf of the Request.

    Declaration

    Swift

    public var lastRequest: URLRequest? { get }
  • Current URLRequest created on behalf of the Request.

    Declaration

    Swift

    public var request: URLRequest? { get }
  • URLRequests from all of the URLSessionTasks executed on behalf of the Request.

    Declaration

    Swift

    public var performedRequests: [URLRequest] { get }
  • HTTPURLResponse received from the server, if any. If the Request was retried, this is the response of the last URLSessionTask.

    Declaration

    Swift

    public var response: HTTPURLResponse? { get }
  • All URLSessionTasks created on behalf of the Request.

    Declaration

    Swift

    public var tasks: [URLSessionTask] { get }
  • First URLSessionTask created on behalf of the Request.

    Declaration

    Swift

    public var firstTask: URLSessionTask? { get }
  • Last URLSessionTask crated on behalf of the Request.

    Declaration

    Swift

    public var lastTask: URLSessionTask? { get }
  • Current URLSessionTask created on behalf of the Request.

    Declaration

    Swift

    public var task: URLSessionTask? { get }
  • All URLSessionTaskMetrics gathered on behalf of the Request. Should correspond to the tasks created.

    Declaration

    Swift

    public var allMetrics: [URLSessionTaskMetrics] { get }
  • First URLSessionTaskMetrics gathered on behalf of the Request.

    Declaration

    Swift

    public var firstMetrics: URLSessionTaskMetrics? { get }
  • Last URLSessionTaskMetrics gathered on behalf of the Request.

    Declaration

    Swift

    public var lastMetrics: URLSessionTaskMetrics? { get }
  • Current URLSessionTaskMetrics gathered on behalf of the Request.

    Declaration

    Swift

    public var metrics: URLSessionTaskMetrics? { get }
  • Number of times the Request has been retried.

    Declaration

    Swift

    public var retryCount: Int { get }
  • Error returned from Alamofire internally, from the network request directly, or any validators executed.

    Declaration

    Swift

    fileprivate(set) public var error: Error? { get set }
  • Default initializer for the Request superclass.

    Declaration

    Swift

    public init(id: UUID = UUID(),
                underlyingQueue: DispatchQueue,
                serializationQueue: DispatchQueue,
                eventMonitor: EventMonitor?,
                interceptor: RequestInterceptor?,
                delegate: RequestDelegate)

    Parameters

    id

    UUID used for the Hashable and Equatable implementations. Defaults to a random UUID.

    underlyingQueue

    DispatchQueue on which all internal Request work is performed.

    serializationQueue

    DispatchQueue on which all serialization work is performed. Targets the underlyingQueue when created by a SessionManager.

    eventMonitor

    EventMonitor used for event callbacks from internal Request actions.

    interceptor

    RequestInterceptor used throughout the request lifecycle.

    delegate

    RequestDelegate that provides an interface to actions not performed by the Request.

  • Cancels the Request. Once cancelled, a Request can no longer be resumed or suspended.

    Declaration

    Swift

    @discardableResult
    public func cancel() -> Self

    Return Value

    The Request.

  • Suspends the Request.

    Declaration

    Swift

    @discardableResult
    public func suspend() -> Self

    Return Value

    The Request.

  • Resumes the Request.

    Declaration

    Swift

    @discardableResult
    public func resume() -> Self

    Return Value

    The Request.

  • Associates a credential using the provided values with the Request.

    Declaration

    Swift

    @discardableResult
    public func authenticate(username: String, password: String, persistence: URLCredential.Persistence = .forSession) -> Self

    Parameters

    username

    The username.

    password

    The password.

    persistence

    The URLCredential.Persistence for the created URLCredential.

    Return Value

    The Request.

  • Associates the provided credential with the Request.

    Declaration

    Swift

    @discardableResult
    public func authenticate(with credential: URLCredential) -> Self

    Parameters

    credential

    The URLCredential.

    Return Value

    The Request.

  • Sets a closure to be called periodically during the lifecycle of the Request as data is read from the server.

    Only the last closure provided is used.

    Declaration

    Swift

    @discardableResult
    public func downloadProgress(queue: DispatchQueue = .main, closure: @escaping ProgressHandler) -> Self

    Parameters

    queue

    The DispatchQueue to execute the closure on. Defaults to .main.

    closure

    The code to be executed periodically as data is read from the server.

    Return Value

    The Request.

  • Sets a closure to be called periodically during the lifecycle of the Request as data is sent to the server.

    Only the last closure provided is used.

    Declaration

    Swift

    @discardableResult
    public func uploadProgress(queue: DispatchQueue = .main, closure: @escaping ProgressHandler) -> Self

    Parameters

    queue

    The DispatchQueue to execute the closure on. Defaults to .main.

    closure

    The closure to be executed periodically as data is sent to the server.

    Return Value

    The Request.

  • Sets the redirect handler for the Request which will be used if a redirect response is encountered.

    Declaration

    Swift

    @discardableResult
    public func redirect(using handler: RedirectHandler) -> Self

    Parameters

    handler

    Return Value

    The Request.

  • Final cleanup step executed when a Request finishes response serialization.

    Declaration

    Swift

    open func cleanup()
  • Posted when a Request is resumed. The Notification contains the resumed Request.

    Declaration

    Swift

    static let didResumeNotification: Notification.Name
  • Posted when a Request is suspended. The Notification contains the suspended Request.

    Declaration

    Swift

    static let didSuspendNotification: Notification.Name
  • Posted when a Request is cancelled. The Notification contains the cancelled Request.

    Declaration

    Swift

    static let didCancelNotification: Notification.Name
  • Posted when a Request is finished. The Notification contains the completed Request.

    Declaration

    Swift

    static let didFinishNotification: Notification.Name
  • Posted when a URLSessionTask is resumed. The Notification contains the Request associated with the URLSessionTask.

    Declaration

    Swift

    static let didResumeTaskNotification: Notification.Name
  • Posted when a URLSessionTask is suspended. The Notification contains the Request associated with the URLSessionTask.

    Declaration

    Swift

    static let didSuspendTaskNotification: Notification.Name
  • Posted when a URLSessionTask is cancelled. The Notification contains the Request associated with the URLSessionTask.

    Declaration

    Swift

    static let didCancelTaskNotification: Notification.Name
  • Posted when a URLSessionTask is completed. The Notification contains the Request associated with the URLSessionTask.

    Declaration

    Swift

    static let didCompleteTaskNotification: Notification.Name
  • Declaration

    Swift

    public static func == (lhs: Request, rhs: Request) -> Bool
  • Declaration

    Swift

    public func hash(into hasher: inout Hasher)
  • A textual representation of this instance, including the HTTPMethod and URL if the URLRequest has been created, as well as the response status code, if a response has been received.

    Declaration

    Swift

    public var description: String { get }
  • A textual representation of this instance in the form of a cURL command.

    Declaration

    Swift

    public var debugDescription: String { get }
  • Used to represent whether a validation succeeded or failed.

    Declaration

    Swift

    public typealias ValidationResult = AFResult<Void>