docs/Classes/Request.html
public class Request : @unchecked Sendable
extension Request: Equatable
extension Request: Hashable
extension Request: CustomStringConvertible
Request is the common superclass of all Alamofire request types and provides common state, delegate, and callback handling.
`
State
`
State of the Request, with managed transitions between states set when calling resume(), suspend(), or cancel() on the Request.
Swift
public enum State
`
id
`
UUID providing a unique identifier for the Request, used in the Hashable and Equatable conformances.
Swift
public let id: UUID
`
underlyingQueue
`
The serial queue for all internal async actions.
Swift
public let underlyingQueue: DispatchQueue
`
serializationQueue
`
The queue used for all serialization actions. By default it’s a serial queue that targets underlyingQueue.
Swift
public let serializationQueue: DispatchQueue
`
eventMonitor
`
EventMonitor used for event callbacks.
Swift
public var eventMonitor: (any EventMonitor)? { get }
`
interceptor
`
The Request‘s interceptor.
Swift
public var interceptor: (any RequestInterceptor)? { get }
`
shouldAutomaticallyResume
`
Whether the instance should call resume() automatically once the first response handler has been added. Overrides the same setting from Session, if set. nil by default (defers to the Session).
Swift
public let shouldAutomaticallyResume: Bool?
`
delegate
`
The Request‘s delegate.
Swift
public private(set) weak var delegate: (any RequestDelegate)? { get }
`
state
`
State of the Request.
Swift
public var state: State { get }
`
isInitialized
`
Returns whether state is .initialized.
Swift
public var isInitialized: Bool { get }
`
isResumed
`
Returns whether state is .resumed.
Swift
public var isResumed: Bool { get }
`
isSuspended
`
Returns whether state is .suspended.
Swift
public var isSuspended: Bool { get }
`
isCancelled
`
Returns whether state is .cancelled.
Swift
public var isCancelled: Bool { get }
`
isFinished
`
Returns whether state is .finished.
Swift
public var isFinished: Bool { get }
`
ProgressHandler
`
Closure type executed when monitoring the upload or download progress of a request.
Swift
public typealias ProgressHandler = @Sendable (_ progress: Progress) -> Void
`
uploadProgress
`
Progress of the upload of the body of the executed URLRequest. Reset to 0 if the Request is retried.
Swift
public let uploadProgress: Progress
`
downloadProgress
`
Progress of the download of any response data. Reset to 0 if the Request is retried.
Swift
public let downloadProgress: Progress
`
uploadProgressHandler
`
ProgressHandler called when uploadProgress is updated, on the provided DispatchQueue.
Swift
public internal(set) var uploadProgressHandler: (handler: ProgressHandler, queue: DispatchQueue)? { get set }
`
downloadProgressHandler
`
ProgressHandler called when downloadProgress is updated, on the provided DispatchQueue.
Swift
public internal(set) var downloadProgressHandler: (handler: ProgressHandler, queue: DispatchQueue)? { get set }
`
redirectHandler
`
RedirectHandler set on the instance.
Swift
public internal(set) var redirectHandler: (any RedirectHandler)? { get set }
`
cachedResponseHandler
`
CachedResponseHandler set on the instance.
Swift
public internal(set) var cachedResponseHandler: (any CachedResponseHandler)? { get set }
`
credential
`
URLCredential used for authentication challenges. Created by calling one of the authenticate methods.
Swift
public internal(set) var credential: URLCredential? { get set }
`
requests
`
All URLRequests created on behalf of the Request, including original and adapted requests.
Swift
public var requests: [URLRequest] { get }
`
firstRequest
`
First URLRequest created on behalf of the Request. May not be the first one actually executed.
Swift
public var firstRequest: URLRequest? { get }
`
lastRequest
`
Last URLRequest created on behalf of the Request.
Swift
public var lastRequest: URLRequest? { get }
`
request
`
Current URLRequest created on behalf of the Request.
Swift
public var request: URLRequest? { get }
`
performedRequests
`
URLRequests from all of the URLSessionTasks executed on behalf of the Request. May be different from requests due to URLSession manipulation.
Swift
public var performedRequests: [URLRequest] { get }
`
response
`
HTTPURLResponse received from the server, if any. If the Request was retried, this is the response of the last URLSessionTask.
Swift
public var response: HTTPURLResponse? { get }
`
tasks
`
All URLSessionTasks created on behalf of the Request.
Swift
public var tasks: [URLSessionTask] { get }
`
firstTask
`
First URLSessionTask created on behalf of the Request.
Swift
public var firstTask: URLSessionTask? { get }
`
lastTask
`
Last URLSessionTask created on behalf of the Request.
Swift
public var lastTask: URLSessionTask? { get }
`
task
`
Current URLSessionTask created on behalf of the Request.
Swift
public var task: URLSessionTask? { get }
`
allMetrics
`
All URLSessionTaskMetrics gathered on behalf of the Request. Should correspond to the tasks created.
Swift
public var allMetrics: [URLSessionTaskMetrics] { get }
`
firstMetrics
`
First URLSessionTaskMetrics gathered on behalf of the Request.
Swift
public var firstMetrics: URLSessionTaskMetrics? { get }
`
lastMetrics
`
Last URLSessionTaskMetrics gathered on behalf of the Request.
Swift
public var lastMetrics: URLSessionTaskMetrics? { get }
`
metrics
`
Current URLSessionTaskMetrics gathered on behalf of the Request.
Swift
public var metrics: URLSessionTaskMetrics? { get }
`
retryCount
`
Number of times the Request has been retried.
Swift
public var retryCount: Int { get }
`
error
`
Error returned from Alamofire internally, from the network request directly, or any validators executed.
Swift
public internal(set) var error: AFError? { get set }
`
cancel()
`
Cancels the instance. Once cancelled, a Request can no longer be resumed or suspended.
Swift
@discardableResult
public func cancel() -> Self
The instance.
`
suspend()
`
Suspends the instance.
Swift
@discardableResult
public func suspend() -> Self
The instance.
`
resume()
`
Resumes the instance.
Swift
@discardableResult
public func resume() -> Self
The instance.
`
authenticate(username:password:persistence:)
`
Associates a credential using the provided values with the instance.
Swift
@discardableResult
public func authenticate(username: String, password: String, persistence: URLCredential.Persistence = .forSession) -> Self
| username |
The username.
|
| password |
The password.
|
| persistence |
The URLCredential.Persistence for the created URLCredential. .forSession by default.
|
The instance.
`
authenticate(with:)
`
Associates the provided credential with the instance.
Swift
@discardableResult
public func authenticate(with credential: URLCredential) -> Self
| credential |
The URLCredential.
|
The instance.
`
downloadProgress(queue:closure:)
`
Sets a closure to be called periodically during the lifecycle of the instance as data is read from the server.
Note
Only the last closure provided is used.
Swift
@discardableResult
@preconcurrency
public func downloadProgress(queue: DispatchQueue = .main, closure: @escaping ProgressHandler) -> Self
| queue |
The DispatchQueue to execute the closure on. .main by default.
|
| closure |
The closure to be executed periodically as data is read from the server.
|
The instance.
`
uploadProgress(queue:closure:)
`
Sets a closure to be called periodically during the lifecycle of the instance as data is sent to the server.
Note
Only the last closure provided is used.
Swift
@discardableResult
@preconcurrency
public func uploadProgress(queue: DispatchQueue = .main, closure: @escaping ProgressHandler) -> Self
| queue |
The DispatchQueue to execute the closure on. .main by default.
|
| closure |
The closure to be executed periodically as data is sent to the server.
|
The instance.
`
redirect(using:)
`
Sets the redirect handler for the instance which will be used if a redirect response is encountered.
Note
Attempting to set the redirect handler more than once is a logic error and will crash.
Swift
@discardableResult
@preconcurrency
public func redirect(using handler: any RedirectHandler) -> Self
| handler |
The RedirectHandler.
|
The instance.
`
cacheResponse(using:)
`
Sets the cached response handler for the Request which will be used when attempting to cache a response.
Note
Attempting to set the cache handler more than once is a logic error and will crash.
Swift
@discardableResult
@preconcurrency
public func cacheResponse(using handler: any CachedResponseHandler) -> Self
| handler |
The CachedResponseHandler.
|
The instance.
`
cURLDescription(on:calling:)
`
Sets a handler to be called when the cURL description of the request is available.
Note
When waiting for a Request‘s URLRequest to be created, only the last handler will be called.
Swift
@discardableResult
@preconcurrency
public func cURLDescription(on queue: DispatchQueue, calling handler: @escaping @Sendable (String) -> Void) -> Self
| queue |
DispatchQueue on which handler will be called.
|
| handler |
Closure to be called when the cURL description is available.
|
The instance.
`
cURLDescription(calling:)
`
Sets a handler to be called when the cURL description of the request is available.
Note
When waiting for a Request‘s URLRequest to be created, only the last handler will be called.
Swift
@discardableResult
@preconcurrency
public func cURLDescription(calling handler: @escaping @Sendable (String) -> Void) -> Self
| handler |
Closure to be called when the cURL description is available. Called on the instance’s underlyingQueue by default.
|
The instance.
`
onURLRequestCreation(on:perform:)
`
Sets a closure to called whenever Alamofire creates a URLRequest for this instance.
Note
This closure will be called multiple times if the instance adapts incoming URLRequests or is retried.
Swift
@discardableResult
@preconcurrency
public func onURLRequestCreation(on queue: DispatchQueue = .main, perform handler: @escaping @Sendable (URLRequest) -> Void) -> Self
| queue |
DispatchQueue on which handler will be called. .main by default.
|
| handler |
Closure to be called when a URLRequest is available.
|
The instance.
`
onURLSessionTaskCreation(on:perform:)
`
Sets a closure to be called whenever the instance creates a URLSessionTask.
Note
This API should only be used to provide URLSessionTasks to existing API, like NSFileProvider. It SHOULD NOT be used to interact with tasks directly, as that may be break Alamofire features. Additionally, this closure may be called multiple times if the instance is retried.
Swift
@discardableResult
@preconcurrency
public func onURLSessionTaskCreation(on queue: DispatchQueue = .main, perform handler: @escaping @Sendable (URLSessionTask) -> Void) -> Self
| queue |
DispatchQueue on which handler will be called. .main by default.
|
| handler |
Closure to be called when the URLSessionTask is available.
|
The instance.
`
interceptor(_:)
`
Adds a RequestInterceptor for this instance, called after the interceptors of the parent Session.
Swift
@discardableResult
@preconcurrency
public func interceptor(_ interceptor: any RequestInterceptor) -> Self
| interceptor |
RequestInterceptor to add.
|
The instance.
`
adapt(using:)
`
Adds a RequestAdapter for this instance, called after the adapters of the parent Session.
Swift
@discardableResult
@preconcurrency
public func adapt(using adapter: any RequestAdapter) -> Self
| adapter |
RequestAdapter to be called.
|
The instance.
`
retry(using:)
`
Adds a RequestRetrier for this instance, called after the retriers of the parent Session.
Swift
@discardableResult
@preconcurrency
public func retry(using retrier: any RequestRetrier) -> Self
| retrier |
RequestRetrier to add.
|
The instance.
`
eventMonitor(_:)
`
Adds an EventMonitor for this instance, called after the EventMonitors of the parent Session.
Note
Request EventMonitors only receive Request events (see the “Request Events” section of the EventMonitor protocol). URLSession events are only sent at the Session level.
Swift
@discardableResult
@preconcurrency
public func eventMonitor(_ eventMonitor: any EventMonitor) -> Self
| eventMonitor |
EventMonitor to add.
|
The instance.
`
didResumeNotification
`
Posted when a Request is resumed. The Notification contains the resumed Request.
Swift
public static let didResumeNotification: Notification.Name
`
didSuspendNotification
`
Posted when a Request is suspended. The Notification contains the suspended Request.
Swift
public static let didSuspendNotification: Notification.Name
`
didCancelNotification
`
Posted when a Request is cancelled. The Notification contains the cancelled Request.
Swift
public static let didCancelNotification: Notification.Name
`
didFinishNotification
`
Posted when a Request is finished. The Notification contains the completed Request.
Swift
public static let didFinishNotification: Notification.Name
`
didResumeTaskNotification
`
Posted when a URLSessionTask is resumed. The Notification contains the Request associated with the URLSessionTask.
Swift
public static let didResumeTaskNotification: Notification.Name
`
didSuspendTaskNotification
`
Posted when a URLSessionTask is suspended. The Notification contains the Request associated with the URLSessionTask.
Swift
public static let didSuspendTaskNotification: Notification.Name
`
didCancelTaskNotification
`
Posted when a URLSessionTask is cancelled. The Notification contains the Request associated with the URLSessionTask.
Swift
public static let didCancelTaskNotification: Notification.Name
`
didCompleteTaskNotification
`
Posted when a URLSessionTask is completed. The Notification contains the Request associated with the URLSessionTask.
Swift
public static let didCompleteTaskNotification: Notification.Name
`
ResponseDisposition
`
Type indicating how a DataRequest or DataStreamRequest should proceed after receiving an HTTPURLResponse.
Swift
public enum ResponseDisposition : Sendable
`
==(_:_:)
`
Swift
public static func == (lhs: Request, rhs: Request) -> Bool
`
hash(into:)
`
Swift
public func hash(into hasher: inout Hasher)
`
description
`
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.
Swift
public var description: String { get }
`
cURLDescription()
`
cURL representation of the instance.
Swift
public func cURLDescription() -> String
The cURL equivalent of the instance.
`
uploadProgress(bufferingPolicy:)
`
Creates a StreamOf<Progress> for the instance’s upload progress.
Swift
public func uploadProgress(bufferingPolicy: StreamOf<Progress>.BufferingPolicy = .unbounded) -> StreamOf<Progress>
| bufferingPolicy |
BufferingPolicy that determines the stream’s buffering behavior..unbounded by default.
|
The StreamOf<Progress>.
`
downloadProgress(bufferingPolicy:)
`
Creates a StreamOf<Progress> for the instance’s download progress.
Swift
public func downloadProgress(bufferingPolicy: StreamOf<Progress>.BufferingPolicy = .unbounded) -> StreamOf<Progress>
| bufferingPolicy |
BufferingPolicy that determines the stream’s buffering behavior..unbounded by default.
|
The StreamOf<Progress>.
`
urlRequests(bufferingPolicy:)
`
Creates a StreamOf<URLRequest> for the URLRequests produced for the instance.
Swift
public func urlRequests(bufferingPolicy: StreamOf<URLRequest>.BufferingPolicy = .unbounded) -> StreamOf<URLRequest>
| bufferingPolicy |
BufferingPolicy that determines the stream’s buffering behavior..unbounded by default.
|
The StreamOf<URLRequest>.
`
urlSessionTasks(bufferingPolicy:)
`
Creates a StreamOf<URLSessionTask> for the URLSessionTasks produced for the instance.
Swift
public func urlSessionTasks(bufferingPolicy: StreamOf<URLSessionTask>.BufferingPolicy = .unbounded) -> StreamOf<URLSessionTask>
| bufferingPolicy |
BufferingPolicy that determines the stream’s buffering behavior..unbounded by default.
|
The StreamOf<URLSessionTask>.
`
cURLDescriptions(bufferingPolicy:)
`
Creates a StreamOf<String> for the cURL descriptions produced for the instance.
Swift
public func cURLDescriptions(bufferingPolicy: StreamOf<String>.BufferingPolicy = .unbounded) -> StreamOf<String>
| bufferingPolicy |
BufferingPolicy that determines the stream’s buffering behavior..unbounded by default.
|
The StreamOf<String>.
`
ValidationResult
`
Used to represent whether a validation succeeded or failed.
Swift
public typealias ValidationResult = Result<Void, any (Error & Sendable)>