Session
open class Session : @unchecked Sendable
extension Session: RequestDelegate
Session
creates and manages Alamofire’s Request
types during their lifetimes. It also provides common
functionality for all Request
s, including queuing, interception, trust management, redirect handling, and response
cache handling.
-
Shared singleton instance used by all
AF.request
APIs. Cannot be modified.Declaration
Swift
public static let `default`: Session
-
Underlying
URLSession
used to createURLSessionTasks
for this instance, and for which this instance’sdelegate
handlesURLSessionDelegate
callbacks.Note
This instance should NOT be used to interact with the underlyingURLSessionTask
s. Doing so will break internal Alamofire logic that tracks those tasks.Declaration
Swift
public let session: URLSession
-
Instance’s
SessionDelegate
, which handles theURLSessionDelegate
methods andRequest
interaction.Declaration
Swift
public let delegate: SessionDelegate
-
Root
DispatchQueue
for all internal callbacks and state update. MUST be a serial queue.Declaration
Swift
public let rootQueue: DispatchQueue
-
Value determining whether this instance automatically calls
resume()
on all createdRequest
s.Declaration
Swift
public let startRequestsImmediately: Bool
-
DispatchQueue
on whichURLRequest
s are created asynchronously. By default this queue usesrootQueue
as itstarget
, but a separate queue can be used if request creation is determined to be a bottleneck. Always profile and test before introducing an additional queue.Declaration
Swift
public let requestQueue: DispatchQueue
-
DispatchQueue
passed to allRequest
s on which they perform their response serialization. By default this queue usesrootQueue
as itstarget
but a separate queue can be used if response serialization is determined to be a bottleneck. Always profile and test before introducing an additional queue.Declaration
Swift
public let serializationQueue: DispatchQueue
-
RequestInterceptor
used for allRequest
created by the instance.RequestInterceptor
s can also be set on a per-Request
basis, in which case theRequest
‘s interceptor takes precedence over this value.Declaration
Swift
public let interceptor: (any RequestInterceptor)?
-
ServerTrustManager
instance used to evaluate all trust challenges and provide certificate and key pinning.Declaration
Swift
public let serverTrustManager: ServerTrustManager?
-
RedirectHandler
instance used to provide customization for request redirection.Declaration
Swift
public let redirectHandler: (any RedirectHandler)?
-
CachedResponseHandler
instance used to provide customization of cached response handling.Declaration
Swift
public let cachedResponseHandler: (any CachedResponseHandler)?
-
CompositeEventMonitor
used to compose any passedEventMonitor
s.Declaration
Swift
public let eventMonitor: CompositeEventMonitor
-
EventMonitor
s included in all instances unless overwritten.[AlamofireNotifications()]
by default.Declaration
Swift
@available(*, deprecated, message: "Use [AlamofireNotifications(﹚] directly.") public let defaultEventMonitors: [any EventMonitor]
-
init(session:
delegate: rootQueue: startRequestsImmediately: requestQueue: serializationQueue: interceptor: serverTrustManager: redirectHandler: cachedResponseHandler: eventMonitors: ) Creates a
Session
from aURLSession
and other parameters.Note
When passing a
URLSession
, you must create theURLSession
with a specificdelegateQueue
value and pass thedelegateQueue
‘sunderlyingQueue
as therootQueue
parameter of this initializer.Declaration
Swift
public init(session: URLSession, delegate: SessionDelegate, rootQueue: DispatchQueue, startRequestsImmediately: Bool = true, requestQueue: DispatchQueue? = nil, serializationQueue: DispatchQueue? = nil, interceptor: (any RequestInterceptor)? = nil, serverTrustManager: ServerTrustManager? = nil, redirectHandler: (any RedirectHandler)? = nil, cachedResponseHandler: (any CachedResponseHandler)? = nil, eventMonitors: [any EventMonitor] = [AlamofireNotifications()])
Parameters
session
Underlying
URLSession
for this instance.delegate
SessionDelegate
that handlessession
‘s delegate callbacks as well asRequest
interaction.rootQueue
Root
DispatchQueue
for all internal callbacks and state updates. MUST be a serial queue.startRequestsImmediately
requestQueue
DispatchQueue
on which to performURLRequest
creation. By default this queue will use therootQueue
as itstarget
. A separate queue can be used if it’s determined request creation is a bottleneck, but that should only be done after careful testing and profiling.nil
by default.serializationQueue
DispatchQueue
on which to perform all response serialization. By default this queue will use therootQueue
as itstarget
. A separate queue can be used if it’s determined response serialization is a bottleneck, but that should only be done after careful testing and profiling.nil
by default.interceptor
RequestInterceptor
to be used for allRequest
s created by this instance.nil
by default.serverTrustManager
ServerTrustManager
to be used for all trust evaluations by this instance.nil
by default.redirectHandler
RedirectHandler
to be used by allRequest
s created by this instance.nil
by default.cachedResponseHandler
CachedResponseHandler
to be used by allRequest
s created by this instance.nil
by default.eventMonitors
EventMonitor
s used by the instance.[AlamofireNotifications()]
by default. -
init(configuration:
delegate: rootQueue: startRequestsImmediately: requestQueue: serializationQueue: interceptor: serverTrustManager: redirectHandler: cachedResponseHandler: eventMonitors: ) Creates a
Session
from aURLSessionConfiguration
.Note
This initializer lets Alamofire handle the creation of the underlying
URLSession
and itsdelegateQueue
, and is the recommended initializer for most uses.Declaration
Swift
public convenience init(configuration: URLSessionConfiguration = URLSessionConfiguration.af.default, delegate: SessionDelegate = SessionDelegate(), rootQueue: DispatchQueue = DispatchQueue(label: "org.alamofire.session.rootQueue"), startRequestsImmediately: Bool = true, requestQueue: DispatchQueue? = nil, serializationQueue: DispatchQueue? = nil, interceptor: (any RequestInterceptor)? = nil, serverTrustManager: ServerTrustManager? = nil, redirectHandler: (any RedirectHandler)? = nil, cachedResponseHandler: (any CachedResponseHandler)? = nil, eventMonitors: [any EventMonitor] = [AlamofireNotifications()])
Parameters
configuration
URLSessionConfiguration
to be used to create the underlyingURLSession
. Changes to this value after being passed to this initializer will have no effect.URLSessionConfiguration.af.default
by default.delegate
SessionDelegate
that handlessession
‘s delegate callbacks as well asRequest
interaction.SessionDelegate()
by default.rootQueue
Root
DispatchQueue
for all internal callbacks and state updates. MUST be a serial queue.DispatchQueue(label: "org.alamofire.session.rootQueue")
by default.startRequestsImmediately
requestQueue
DispatchQueue
on which to performURLRequest
creation. By default this queue will use therootQueue
as itstarget
. A separate queue can be used if it’s determined request creation is a bottleneck, but that should only be done after careful testing and profiling.nil
by default.serializationQueue
DispatchQueue
on which to perform all response serialization. By default this queue will use therootQueue
as itstarget
. A separate queue can be used if it’s determined response serialization is a bottleneck, but that should only be done after careful testing and profiling.nil
by default.interceptor
RequestInterceptor
to be used for allRequest
s created by this instance.nil
by default.serverTrustManager
ServerTrustManager
to be used for all trust evaluations by this instance.nil
by default.redirectHandler
RedirectHandler
to be used by allRequest
s created by this instance.nil
by default.cachedResponseHandler
CachedResponseHandler
to be used by allRequest
s created by this instance.nil
by default.eventMonitors
EventMonitor
s used by the instance.[AlamofireNotifications()]
by default.
-
Perform an action on all active
Request
s.Note
The provided
action
closure is performed asynchronously, meaning that someRequest
s may complete and be unavailable by time it runs. Additionally, this action is performed on the instances’srootQueue
, so care should be taken that actions are fast. Once the work on theRequest
s is complete, any additional work should be performed on another queue.Declaration
Swift
public func withAllRequests(perform action: @escaping @Sendable (Set<Request>) -> Void)
Parameters
action
Closure to perform with all
Request
s. -
Cancel all active
Request
s, optionally calling a completion handler when complete.Declaration
Swift
public func cancelAllRequests(completingOnQueue queue: DispatchQueue = .main, completion: (@Sendable () -> Void)? = nil)
Parameters
queue
DispatchQueue
on which the completion handler is run..main
by default.completion
Closure to be called when all
Request
s have been cancelled.
-
Closure which provides a
URLRequest
for mutation.Declaration
Swift
public typealias RequestModifier = @Sendable (inout URLRequest) throws -> Void
-
Creates a
DataRequest
from aURLRequest
created using the passed components and aRequestInterceptor
.Declaration
Swift
open func request(_ convertible: any URLConvertible, method: HTTPMethod = .get, parameters: Parameters? = nil, encoding: any ParameterEncoding = URLEncoding.default, headers: HTTPHeaders? = nil, interceptor: (any RequestInterceptor)? = nil, requestModifier: RequestModifier? = nil) -> DataRequest
Parameters
convertible
URLConvertible
value to be used as theURLRequest
‘sURL
.method
HTTPMethod
for theURLRequest
..get
by default.parameters
Parameters
(a.k.a.[String: Any]
) value to be encoded into theURLRequest
.nil
by default.encoding
ParameterEncoding
to be used to encode theparameters
value into theURLRequest
.URLEncoding.default
by default.headers
HTTPHeaders
value to be added to theURLRequest
.nil
by default.interceptor
RequestInterceptor
value to be used by the returnedDataRequest
.nil
by default.requestModifier
RequestModifier
which will be applied to theURLRequest
created from the provided parameters.nil
by default.Return Value
The created
DataRequest
. -
Creates a
DataRequest
from aURLRequest
created using the passed components,Encodable
parameters, and aRequestInterceptor
.Declaration
Swift
open func request<Parameters: Encodable & Sendable>(_ convertible: any URLConvertible, method: HTTPMethod = .get, parameters: Parameters? = nil, encoder: any ParameterEncoder = URLEncodedFormParameterEncoder.default, headers: HTTPHeaders? = nil, interceptor: (any RequestInterceptor)? = nil, requestModifier: RequestModifier? = nil) -> DataRequest
Parameters
convertible
URLConvertible
value to be used as theURLRequest
‘sURL
.method
HTTPMethod
for theURLRequest
..get
by default.parameters
Encodable
value to be encoded into theURLRequest
.nil
by default.encoder
ParameterEncoder
to be used to encode theparameters
value into theURLRequest
.URLEncodedFormParameterEncoder.default
by default.headers
HTTPHeaders
value to be added to theURLRequest
.nil
by default.interceptor
RequestInterceptor
value to be used by the returnedDataRequest
.nil
by default.requestModifier
RequestModifier
which will be applied to theURLRequest
created from the provided parameters.nil
by default.Return Value
The created
DataRequest
. -
Creates a
DataRequest
from aURLRequestConvertible
value and aRequestInterceptor
.Declaration
Swift
open func request(_ convertible: any URLRequestConvertible, interceptor: (any RequestInterceptor)? = nil) -> DataRequest
Parameters
convertible
URLRequestConvertible
value to be used to create theURLRequest
.interceptor
RequestInterceptor
value to be used by the returnedDataRequest
.nil
by default.Return Value
The created
DataRequest
.
-
streamRequest(_:
method: parameters: encoder: headers: automaticallyCancelOnStreamError: interceptor: requestModifier: ) Creates a
DataStreamRequest
from the passed components,Encodable
parameters, andRequestInterceptor
.Declaration
Swift
open func streamRequest<Parameters: Encodable & Sendable>(_ convertible: any URLConvertible, method: HTTPMethod = .get, parameters: Parameters? = nil, encoder: any ParameterEncoder = URLEncodedFormParameterEncoder.default, headers: HTTPHeaders? = nil, automaticallyCancelOnStreamError: Bool = false, interceptor: (any RequestInterceptor)? = nil, requestModifier: RequestModifier? = nil) -> DataStreamRequest
Parameters
convertible
URLConvertible
value to be used as theURLRequest
‘sURL
.method
HTTPMethod
for theURLRequest
..get
by default.parameters
Encodable
value to be encoded into theURLRequest
.nil
by default.encoder
ParameterEncoder
to be used to encode theparameters
value into theURLRequest
.URLEncodedFormParameterEncoder.default
by default.headers
HTTPHeaders
value to be added to theURLRequest
.nil
by default.automaticallyCancelOnStreamError
Bool
indicating whether the instance should be canceled when anError
is thrown while serializing streamData
.false
by default.interceptor
RequestInterceptor
value to be used by the returnedDataRequest
.nil
by default.requestModifier
RequestModifier
which will be applied to theURLRequest
created from the provided parameters.nil
by default.Return Value
The created
DataStream
request. -
Creates a
DataStreamRequest
from the passed components andRequestInterceptor
.Declaration
Swift
open func streamRequest(_ convertible: any URLConvertible, method: HTTPMethod = .get, headers: HTTPHeaders? = nil, automaticallyCancelOnStreamError: Bool = false, interceptor: (any RequestInterceptor)? = nil, requestModifier: RequestModifier? = nil) -> DataStreamRequest
Parameters
convertible
URLConvertible
value to be used as theURLRequest
‘sURL
.method
HTTPMethod
for theURLRequest
..get
by default.headers
HTTPHeaders
value to be added to theURLRequest
.nil
by default.automaticallyCancelOnStreamError
Bool
indicating whether the instance should be canceled when anError
is thrown while serializing streamData
.false
by default.interceptor
RequestInterceptor
value to be used by the returnedDataRequest
.nil
by default.requestModifier
RequestModifier
which will be applied to theURLRequest
created from the provided parameters.nil
by default.Return Value
The created
DataStream
request. -
Creates a
DataStreamRequest
from the passedURLRequestConvertible
value andRequestInterceptor
.Declaration
Swift
open func streamRequest(_ convertible: any URLRequestConvertible, automaticallyCancelOnStreamError: Bool = false, interceptor: (any RequestInterceptor)? = nil) -> DataStreamRequest
Parameters
convertible
URLRequestConvertible
value to be used to create theURLRequest
.automaticallyCancelOnStreamError
Bool
indicating whether the instance should be canceled when anError
is thrown while serializing streamData
.false
by default.interceptor
RequestInterceptor
value to be used by the returnedDataRequest
.nil
by default.Return Value
The created
DataStreamRequest
.
-
Creates a
DownloadRequest
using aURLRequest
created using the passed components,RequestInterceptor
, andDestination
.Declaration
Swift
open func download(_ convertible: any URLConvertible, method: HTTPMethod = .get, parameters: Parameters? = nil, encoding: any ParameterEncoding = URLEncoding.default, headers: HTTPHeaders? = nil, interceptor: (any RequestInterceptor)? = nil, requestModifier: RequestModifier? = nil, to destination: DownloadRequest.Destination? = nil) -> DownloadRequest
Parameters
convertible
URLConvertible
value to be used as theURLRequest
‘sURL
.method
HTTPMethod
for theURLRequest
..get
by default.parameters
Parameters
(a.k.a.[String: Any]
) value to be encoded into theURLRequest
.nil
by default.encoding
ParameterEncoding
to be used to encode theparameters
value into theURLRequest
. Defaults toURLEncoding.default
.headers
HTTPHeaders
value to be added to theURLRequest
.nil
by default.interceptor
RequestInterceptor
value to be used by the returnedDataRequest
.nil
by default.requestModifier
RequestModifier
which will be applied to theURLRequest
created from the provided parameters.nil
by default.destination
DownloadRequest.Destination
closure used to determine how and where the downloaded file should be moved.nil
by default.Return Value
The created
DownloadRequest
. -
Creates a
DownloadRequest
from aURLRequest
created using the passed components,Encodable
parameters, and aRequestInterceptor
.Declaration
Swift
open func download<Parameters: Encodable & Sendable>(_ convertible: any URLConvertible, method: HTTPMethod = .get, parameters: Parameters? = nil, encoder: any ParameterEncoder = URLEncodedFormParameterEncoder.default, headers: HTTPHeaders? = nil, interceptor: (any RequestInterceptor)? = nil, requestModifier: RequestModifier? = nil, to destination: DownloadRequest.Destination? = nil) -> DownloadRequest
Parameters
convertible
URLConvertible
value to be used as theURLRequest
‘sURL
.method
HTTPMethod
for theURLRequest
..get
by default.parameters
Value conforming to
Encodable
to be encoded into theURLRequest
.nil
by default.encoder
ParameterEncoder
to be used to encode theparameters
value into theURLRequest
. Defaults toURLEncodedFormParameterEncoder.default
.headers
HTTPHeaders
value to be added to theURLRequest
.nil
by default.interceptor
RequestInterceptor
value to be used by the returnedDataRequest
.nil
by default.requestModifier
RequestModifier
which will be applied to theURLRequest
created from the provided parameters.nil
by default.destination
DownloadRequest.Destination
closure used to determine how and where the downloaded file should be moved.nil
by default.Return Value
The created
DownloadRequest
. -
Creates a
DownloadRequest
from aURLRequestConvertible
value, aRequestInterceptor
, and aDestination
.Declaration
Swift
open func download(_ convertible: any URLRequestConvertible, interceptor: (any RequestInterceptor)? = nil, to destination: DownloadRequest.Destination? = nil) -> DownloadRequest
Parameters
convertible
URLRequestConvertible
value to be used to create theURLRequest
.interceptor
RequestInterceptor
value to be used by the returnedDataRequest
.nil
by default.destination
DownloadRequest.Destination
closure used to determine how and where the downloaded file should be moved.nil
by default.Return Value
The created
DownloadRequest
. -
Creates a
DownloadRequest
from theresumeData
produced from a previously cancelledDownloadRequest
, as well as aRequestInterceptor
, and aDestination
.Note
If
destination
is not specified, the download will be moved to a temporary location determined by Alamofire. The file will not be deleted until the system purges the temporary files.Note
On some versions of all Apple platforms (iOS 10 - 10.2, macOS 10.12 - 10.12.2, tvOS 10 - 10.1, watchOS 3 - 3.1.1),
resumeData
is broken on background URL session configurations. There’s an underlying bug in theresumeData
generation logic where the data is written incorrectly and will always fail to resume the download. For more information about the bug and possible workarounds, please refer to the this Stack Overflow post.Declaration
Swift
open func download(resumingWith data: Data, interceptor: (any RequestInterceptor)? = nil, to destination: DownloadRequest.Destination? = nil) -> DownloadRequest
Parameters
data
The resume data from a previously cancelled
DownloadRequest
orURLSessionDownloadTask
.interceptor
RequestInterceptor
value to be used by the returnedDataRequest
.nil
by default.destination
DownloadRequest.Destination
closure used to determine how and where the downloaded file should be moved.nil
by default.Return Value
The created
DownloadRequest
.
-
Creates an
UploadRequest
for the givenData
,URLRequest
components, andRequestInterceptor
.Declaration
Swift
open func upload(_ data: Data, to convertible: any URLConvertible, method: HTTPMethod = .post, headers: HTTPHeaders? = nil, interceptor: (any RequestInterceptor)? = nil, fileManager: FileManager = .default, requestModifier: RequestModifier? = nil) -> UploadRequest
Parameters
data
The
Data
to upload.convertible
URLConvertible
value to be used as theURLRequest
‘sURL
.method
HTTPMethod
for theURLRequest
..post
by default.headers
HTTPHeaders
value to be added to theURLRequest
.nil
by default.interceptor
RequestInterceptor
value to be used by the returnedDataRequest
.nil
by default.fileManager
FileManager
instance to be used by the returnedUploadRequest
..default
instance by default.requestModifier
RequestModifier
which will be applied to theURLRequest
created from the provided parameters.nil
by default.Return Value
The created
UploadRequest
. -
Creates an
UploadRequest
for the givenData
using theURLRequestConvertible
value andRequestInterceptor
.Declaration
Swift
open func upload(_ data: Data, with convertible: any URLRequestConvertible, interceptor: (any RequestInterceptor)? = nil, fileManager: FileManager = .default) -> UploadRequest
Parameters
data
The
Data
to upload.convertible
URLRequestConvertible
value to be used to create theURLRequest
.interceptor
RequestInterceptor
value to be used by the returnedDataRequest
.nil
by default.fileManager
FileManager
instance to be used by the returnedUploadRequest
..default
instance by default.Return Value
The created
UploadRequest
.
-
Creates an
UploadRequest
for the file at the given fileURL
, using aURLRequest
from the provided components andRequestInterceptor
.Declaration
Swift
open func upload(_ fileURL: URL, to convertible: any URLConvertible, method: HTTPMethod = .post, headers: HTTPHeaders? = nil, interceptor: (any RequestInterceptor)? = nil, fileManager: FileManager = .default, requestModifier: RequestModifier? = nil) -> UploadRequest
Parameters
fileURL
The
URL
of the file to upload.convertible
URLConvertible
value to be used as theURLRequest
‘sURL
.method
HTTPMethod
for theURLRequest
..post
by default.headers
HTTPHeaders
value to be added to theURLRequest
.nil
by default.interceptor
RequestInterceptor
value to be used by the returnedUploadRequest
.nil
by default.fileManager
FileManager
instance to be used by the returnedUploadRequest
..default
instance by default.requestModifier
RequestModifier
which will be applied to theURLRequest
created from the provided parameters.nil
by default.Return Value
The created
UploadRequest
. -
Creates an
UploadRequest
for the file at the given fileURL
using theURLRequestConvertible
value andRequestInterceptor
.Declaration
Swift
open func upload(_ fileURL: URL, with convertible: any URLRequestConvertible, interceptor: (any RequestInterceptor)? = nil, fileManager: FileManager = .default) -> UploadRequest
Parameters
fileURL
The
URL
of the file to upload.convertible
URLRequestConvertible
value to be used to create theURLRequest
.interceptor
RequestInterceptor
value to be used by the returnedDataRequest
.nil
by default.fileManager
FileManager
instance to be used by the returnedUploadRequest
..default
instance by default.Return Value
The created
UploadRequest
.
-
Creates an
UploadRequest
from theInputStream
provided using aURLRequest
from the provided components andRequestInterceptor
.Declaration
Swift
open func upload(_ stream: InputStream, to convertible: any URLConvertible, method: HTTPMethod = .post, headers: HTTPHeaders? = nil, interceptor: (any RequestInterceptor)? = nil, fileManager: FileManager = .default, requestModifier: RequestModifier? = nil) -> UploadRequest
Parameters
stream
The
InputStream
that provides the data to upload.convertible
URLConvertible
value to be used as theURLRequest
‘sURL
.method
HTTPMethod
for theURLRequest
..post
by default.headers
HTTPHeaders
value to be added to theURLRequest
.nil
by default.interceptor
RequestInterceptor
value to be used by the returnedDataRequest
.nil
by default.fileManager
FileManager
instance to be used by the returnedUploadRequest
..default
instance by default.requestModifier
RequestModifier
which will be applied to theURLRequest
created from the provided parameters.nil
by default.Return Value
The created
UploadRequest
. -
Creates an
UploadRequest
from the providedInputStream
using theURLRequestConvertible
value andRequestInterceptor
.Declaration
Swift
open func upload(_ stream: InputStream, with convertible: any URLRequestConvertible, interceptor: (any RequestInterceptor)? = nil, fileManager: FileManager = .default) -> UploadRequest
Parameters
stream
The
InputStream
that provides the data to upload.convertible
URLRequestConvertible
value to be used to create theURLRequest
.interceptor
RequestInterceptor
value to be used by the returnedDataRequest
.nil
by default.fileManager
FileManager
instance to be used by the returnedUploadRequest
..default
instance by default.Return Value
The created
UploadRequest
.
-
Creates an
UploadRequest
for the multipart form data built using a closure and sent using the providedURLRequest
components andRequestInterceptor
.It is important to understand the memory implications of uploading
MultipartFormData
. If the cumulative payload is small, encoding the data in-memory and directly uploading to a server is the by far the most efficient approach. However, if the payload is too large, encoding the data in-memory could cause your app to be terminated. Larger payloads must first be written to disk using input and output streams to keep the memory footprint low, then the data can be uploaded as a stream from the resulting file. Streaming from disk MUST be used for larger payloads such as video content.The
encodingMemoryThreshold
parameter allows Alamofire to automatically determine whether to encode in-memory or stream from disk. If the content length of theMultipartFormData
is below theencodingMemoryThreshold
, encoding takes place in-memory. If the content length exceeds the threshold, the data is streamed to disk during the encoding process. Then the result is uploaded as data or as a stream depending on which encoding technique was used.Declaration
Swift
open func upload(multipartFormData: @escaping (MultipartFormData) -> Void, to url: any URLConvertible, usingThreshold encodingMemoryThreshold: UInt64 = MultipartFormData.encodingMemoryThreshold, method: HTTPMethod = .post, headers: HTTPHeaders? = nil, interceptor: (any RequestInterceptor)? = nil, fileManager: FileManager = .default, requestModifier: RequestModifier? = nil) -> UploadRequest
Parameters
multipartFormData
MultipartFormData
building closure.url
URLConvertible
value to be used as theURLRequest
‘sURL
.encodingMemoryThreshold
Byte threshold used to determine whether the form data is encoded into memory or onto disk before being uploaded.
MultipartFormData.encodingMemoryThreshold
by default.method
HTTPMethod
for theURLRequest
..post
by default.headers
HTTPHeaders
value to be added to theURLRequest
.nil
by default.interceptor
RequestInterceptor
value to be used by the returnedDataRequest
.nil
by default.fileManager
FileManager
to be used if the form data exceeds the memory threshold and is written to disk before being uploaded..default
instance by default.requestModifier
RequestModifier
which will be applied to theURLRequest
created from the provided parameters.nil
by default.Return Value
The created
UploadRequest
. -
Creates an
UploadRequest
using aMultipartFormData
building closure, the providedURLRequestConvertible
value, and aRequestInterceptor
.It is important to understand the memory implications of uploading
MultipartFormData
. If the cumulative payload is small, encoding the data in-memory and directly uploading to a server is the by far the most efficient approach. However, if the payload is too large, encoding the data in-memory could cause your app to be terminated. Larger payloads must first be written to disk using input and output streams to keep the memory footprint low, then the data can be uploaded as a stream from the resulting file. Streaming from disk MUST be used for larger payloads such as video content.The
encodingMemoryThreshold
parameter allows Alamofire to automatically determine whether to encode in-memory or stream from disk. If the content length of theMultipartFormData
is below theencodingMemoryThreshold
, encoding takes place in-memory. If the content length exceeds the threshold, the data is streamed to disk during the encoding process. Then the result is uploaded as data or as a stream depending on which encoding technique was used.Declaration
Swift
open func upload(multipartFormData: @escaping (MultipartFormData) -> Void, with request: any URLRequestConvertible, usingThreshold encodingMemoryThreshold: UInt64 = MultipartFormData.encodingMemoryThreshold, interceptor: (any RequestInterceptor)? = nil, fileManager: FileManager = .default) -> UploadRequest
Parameters
multipartFormData
MultipartFormData
building closure.request
URLRequestConvertible
value to be used to create theURLRequest
.encodingMemoryThreshold
Byte threshold used to determine whether the form data is encoded into memory or onto disk before being uploaded.
MultipartFormData.encodingMemoryThreshold
by default.interceptor
RequestInterceptor
value to be used by the returnedDataRequest
.nil
by default.fileManager
FileManager
to be used if the form data exceeds the memory threshold and is written to disk before being uploaded..default
instance by default.Return Value
The created
UploadRequest
. -
Creates an
UploadRequest
for the prebuiltMultipartFormData
value using the providedURLRequest
components andRequestInterceptor
.It is important to understand the memory implications of uploading
MultipartFormData
. If the cumulative payload is small, encoding the data in-memory and directly uploading to a server is the by far the most efficient approach. However, if the payload is too large, encoding the data in-memory could cause your app to be terminated. Larger payloads must first be written to disk using input and output streams to keep the memory footprint low, then the data can be uploaded as a stream from the resulting file. Streaming from disk MUST be used for larger payloads such as video content.The
encodingMemoryThreshold
parameter allows Alamofire to automatically determine whether to encode in-memory or stream from disk. If the content length of theMultipartFormData
is below theencodingMemoryThreshold
, encoding takes place in-memory. If the content length exceeds the threshold, the data is streamed to disk during the encoding process. Then the result is uploaded as data or as a stream depending on which encoding technique was used.Declaration
Swift
open func upload(multipartFormData: MultipartFormData, to url: any URLConvertible, usingThreshold encodingMemoryThreshold: UInt64 = MultipartFormData.encodingMemoryThreshold, method: HTTPMethod = .post, headers: HTTPHeaders? = nil, interceptor: (any RequestInterceptor)? = nil, fileManager: FileManager = .default, requestModifier: RequestModifier? = nil) -> UploadRequest
Parameters
multipartFormData
MultipartFormData
instance to upload.url
URLConvertible
value to be used as theURLRequest
‘sURL
.encodingMemoryThreshold
Byte threshold used to determine whether the form data is encoded into memory or onto disk before being uploaded.
MultipartFormData.encodingMemoryThreshold
by default.method
HTTPMethod
for theURLRequest
..post
by default.headers
HTTPHeaders
value to be added to theURLRequest
.nil
by default.interceptor
RequestInterceptor
value to be used by the returnedDataRequest
.nil
by default.fileManager
FileManager
to be used if the form data exceeds the memory threshold and is written to disk before being uploaded..default
instance by default.requestModifier
RequestModifier
which will be applied to theURLRequest
created from the provided parameters.nil
by default.Return Value
The created
UploadRequest
. -
Creates an
UploadRequest
for the prebuiltMultipartFormData
value using the providingURLRequestConvertible
value andRequestInterceptor
.It is important to understand the memory implications of uploading
MultipartFormData
. If the cumulative payload is small, encoding the data in-memory and directly uploading to a server is the by far the most efficient approach. However, if the payload is too large, encoding the data in-memory could cause your app to be terminated. Larger payloads must first be written to disk using input and output streams to keep the memory footprint low, then the data can be uploaded as a stream from the resulting file. Streaming from disk MUST be used for larger payloads such as video content.The
encodingMemoryThreshold
parameter allows Alamofire to automatically determine whether to encode in-memory or stream from disk. If the content length of theMultipartFormData
is below theencodingMemoryThreshold
, encoding takes place in-memory. If the content length exceeds the threshold, the data is streamed to disk during the encoding process. Then the result is uploaded as data or as a stream depending on which encoding technique was used.Declaration
Swift
open func upload(multipartFormData: MultipartFormData, with request: any URLRequestConvertible, usingThreshold encodingMemoryThreshold: UInt64 = MultipartFormData.encodingMemoryThreshold, interceptor: (any RequestInterceptor)? = nil, fileManager: FileManager = .default) -> UploadRequest
Parameters
multipartFormData
MultipartFormData
instance to upload.request
URLRequestConvertible
value to be used to create theURLRequest
.encodingMemoryThreshold
Byte threshold used to determine whether the form data is encoded into memory or onto disk before being uploaded.
MultipartFormData.encodingMemoryThreshold
by default.interceptor
RequestInterceptor
value to be used by the returnedDataRequest
.nil
by default.fileManager
FileManager
instance to be used by the returnedUploadRequest
..default
instance by default.Return Value
The created
UploadRequest
.
-
Declaration
Swift
public var sessionConfiguration: URLSessionConfiguration { get }
-
Declaration
Swift
public var startImmediately: Bool { get }
-
Declaration
Swift
public func cleanup(after request: Request)
-
Declaration
Swift
public func retryResult(for request: Request, dueTo error: AFError, completion: @escaping @Sendable (RetryResult) -> Void)
-
Declaration
Swift
public func retryRequest(_ request: Request, withDelay timeDelay: TimeInterval?)