docs/docsets/Alamofire.docset/Contents/Resources/Documents/Classes/DataRequest.html
public class DataRequest : Request, @unchecked Sendable
Request subclass which handles in-memory Data download using URLSessionDataTask.
`
convertible
`
URLRequestConvertible value used to create URLRequests for this instance.
Swift
public let convertible: any URLRequestConvertible
`
data
`
Data read from the server so far.
Swift
public var data: Data? { get }
`
validate(_:)
`
Validates the request, using the specified closure.
Note
If validation fails, subsequent calls to response handlers will have an associated error.
Swift
@discardableResult
@preconcurrency
public func validate(_ validation: @escaping Validation) -> Self
| validation |
Validation closure used to validate the response.
|
The instance.
`
onHTTPResponse(on:perform:)
`
Sets a closure called whenever the DataRequest produces an HTTPURLResponse and providing a completion handler to return a ResponseDisposition value.
Swift
@discardableResult
@_disfavoredOverload
@preconcurrency
public func onHTTPResponse(
on queue: DispatchQueue = .main,
perform handler: @escaping @Sendable (_ response: HTTPURLResponse,
_ completionHandler: @escaping @Sendable (ResponseDisposition) -> Void) -> Void
) -> Self
| queue |
DispatchQueue on which the closure will be called. .main by default.
|
| handler |
Closure called when the instance produces an HTTPURLResponse. The completionHandler provided MUST be called, otherwise the request will never complete.
|
The instance.
`
onHTTPResponse(on:perform:)
`
Sets a closure called whenever the DataRequest produces an HTTPURLResponse.
Swift
@discardableResult
@preconcurrency
public func onHTTPResponse(on queue: DispatchQueue = .main,
perform handler: @escaping @Sendable (HTTPURLResponse) -> Void) -> Self
| queue |
DispatchQueue on which the closure will be called. .main by default.
|
| handler |
Closure called when the instance produces an HTTPURLResponse.
|
The instance.
`
response(queue:completionHandler:)
`
Adds a handler to be called once the request has finished.
Swift
@discardableResult
@preconcurrency
public func response(queue: DispatchQueue = .main, completionHandler: @escaping @Sendable (AFDataResponse<Data?>) -> Void) -> Self
| queue |
The queue on which the completion handler is dispatched. .main by default.
|
| completionHandler |
The code to be executed once the request has finished.
|
The request.
`
response(queue:responseSerializer:completionHandler:)
`
Adds a handler to be called once the request has finished.
Swift
@discardableResult
@preconcurrency
public func response<Serializer: DataResponseSerializerProtocol>(queue: DispatchQueue = .main,
responseSerializer: Serializer,
completionHandler: @escaping @Sendable (AFDataResponse<Serializer.SerializedObject>) -> Void)
-> Self
| queue |
The queue on which the completion handler is dispatched. .main by default
|
| responseSerializer |
The response serializer responsible for serializing the request, response, and data.
|
| completionHandler |
The code to be executed once the request has finished.
|
The request.
`
response(queue:responseSerializer:completionHandler:)
`
Adds a handler to be called once the request has finished.
Swift
@discardableResult
@preconcurrency
public func response<Serializer: ResponseSerializer>(queue: DispatchQueue = .main,
responseSerializer: Serializer,
completionHandler: @escaping @Sendable (AFDataResponse<Serializer.SerializedObject>) -> Void)
-> Self
| queue |
The queue on which the completion handler is dispatched. .main by default
|
| responseSerializer |
The response serializer responsible for serializing the request, response, and data.
|
| completionHandler |
The code to be executed once the request has finished.
|
The request.
`
responseData(queue:dataPreprocessor:emptyResponseCodes:emptyRequestMethods:completionHandler:)
`
Adds a handler using a DataResponseSerializer to be called once the request has finished.
Swift
@discardableResult
@preconcurrency
public func responseData(queue: DispatchQueue = .main,
dataPreprocessor: any DataPreprocessor = DataResponseSerializer.defaultDataPreprocessor,
emptyResponseCodes: Set<Int> = DataResponseSerializer.defaultEmptyResponseCodes,
emptyRequestMethods: Set<HTTPMethod> = DataResponseSerializer.defaultEmptyRequestMethods,
completionHandler: @escaping @Sendable (AFDataResponse<Data>) -> Void) -> Self
| queue |
The queue on which the completion handler is called. .main by default.
|
| dataPreprocessor |
DataPreprocessor which processes the received Data before calling the completionHandler. PassthroughPreprocessor() by default.
|
| emptyResponseCodes |
HTTP status codes for which empty responses are always valid. [204, 205] by default.
|
| emptyRequestMethods |
HTTPMethods for which empty responses are always valid. [.head] by default.
|
| completionHandler |
A closure to be executed once the request has finished.
|
The request.
`
responseString(queue:dataPreprocessor:encoding:emptyResponseCodes:emptyRequestMethods:completionHandler:)
`
Adds a handler using a StringResponseSerializer to be called once the request has finished.
Swift
@discardableResult
@preconcurrency
public func responseString(queue: DispatchQueue = .main,
dataPreprocessor: any DataPreprocessor = StringResponseSerializer.defaultDataPreprocessor,
encoding: String.Encoding? = nil,
emptyResponseCodes: Set<Int> = StringResponseSerializer.defaultEmptyResponseCodes,
emptyRequestMethods: Set<HTTPMethod> = StringResponseSerializer.defaultEmptyRequestMethods,
completionHandler: @escaping @Sendable (AFDataResponse<String>) -> Void) -> Self
| queue |
The queue on which the completion handler is dispatched. .main by default.
|
| dataPreprocessor |
DataPreprocessor which processes the received Data before calling the completionHandler. PassthroughPreprocessor() by default.
|
| encoding |
The string encoding. Defaults to nil, in which case the encoding will be determined from the server response, falling back to the default HTTP character set, ISO-8859-1.
|
| emptyResponseCodes |
HTTP status codes for which empty responses are always valid. [204, 205] by default.
|
| emptyRequestMethods |
HTTPMethods for which empty responses are always valid. [.head] by default.
|
| completionHandler |
A closure to be executed once the request has finished.
|
The request.
`
responseJSON(queue:dataPreprocessor:emptyResponseCodes:emptyRequestMethods:options:completionHandler:)
`
Adds a handler using a JSONResponseSerializer to be called once the request has finished.
Swift
@available(*, deprecated, message: "responseJSON deprecated and will be removed in Alamofire 6. Use responseDecodable instead.")
@discardableResult
@preconcurrency
public func responseJSON(queue: DispatchQueue = .main,
dataPreprocessor: any DataPreprocessor = JSONResponseSerializer.defaultDataPreprocessor,
emptyResponseCodes: Set<Int> = JSONResponseSerializer.defaultEmptyResponseCodes,
emptyRequestMethods: Set<HTTPMethod> = JSONResponseSerializer.defaultEmptyRequestMethods,
options: JSONSerialization.ReadingOptions = .allowFragments,
completionHandler: @escaping @Sendable (AFDataResponse<Any>) -> Void) -> Self
| queue |
The queue on which the completion handler is dispatched. .main by default.
|
| dataPreprocessor |
DataPreprocessor which processes the received Data before calling the completionHandler. PassthroughPreprocessor() by default.
|
| emptyResponseCodes |
HTTP status codes for which empty responses are always valid. [204, 205] by default.
|
| emptyRequestMethods |
HTTPMethods for which empty responses are always valid. [.head] by default.
|
| options |
JSONSerialization.ReadingOptions used when parsing the response. .allowFragments by default.
|
| completionHandler |
A closure to be executed once the request has finished.
|
The request.
`
responseDecodable(of:queue:dataPreprocessor:decoder:emptyResponseCodes:emptyRequestMethods:completionHandler:)
`
Adds a handler using a DecodableResponseSerializer to be called once the request has finished.
Swift
@discardableResult
@preconcurrency
public func responseDecodable<Value>(of type: Value.Type = Value.self,
queue: DispatchQueue = .main,
dataPreprocessor: any DataPreprocessor = DecodableResponseSerializer<Value>.defaultDataPreprocessor,
decoder: any DataDecoder = JSONDecoder(),
emptyResponseCodes: Set<Int> = DecodableResponseSerializer<Value>.defaultEmptyResponseCodes,
emptyRequestMethods: Set<HTTPMethod> = DecodableResponseSerializer<Value>.defaultEmptyRequestMethods,
completionHandler: @escaping @Sendable (AFDataResponse<Value>) -> Void) -> Self where Value: Decodable, Value: Sendable
| type |
Decodable type to decode from response data.
|
| queue |
The queue on which the completion handler is dispatched. .main by default.
|
| dataPreprocessor |
DataPreprocessor which processes the received Data before calling the completionHandler. PassthroughPreprocessor() by default.
|
| decoder |
DataDecoder to use to decode the response. JSONDecoder() by default.
|
| emptyResponseCodes |
HTTP status codes for which empty responses are always valid. [204, 205] by default.
|
| emptyRequestMethods |
HTTPMethods for which empty responses are always valid. [.head] by default.
|
| completionHandler |
A closure to be executed once the request has finished.
|
The request.
`
publishResponse(using:on:)
`
Creates a DataResponsePublisher for this instance using the given ResponseSerializer and DispatchQueue.
Swift
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
public func publishResponse<Serializer: ResponseSerializer, T>(using serializer: Serializer, on queue: DispatchQueue = .main) -> DataResponsePublisher<T>
where Serializer.SerializedObject == T
| serializer |
ResponseSerializer used to serialize response Data.
|
| queue |
DispatchQueue on which the DataResponse will be published. .main by default.
|
The DataResponsePublisher.
`
publishData(queue:preprocessor:emptyResponseCodes:emptyRequestMethods:)
`
Creates a DataResponsePublisher for this instance and uses a DataResponseSerializer to serialize the response.
Swift
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
public func publishData(queue: DispatchQueue = .main,
preprocessor: any DataPreprocessor = DataResponseSerializer.defaultDataPreprocessor,
emptyResponseCodes: Set<Int> = DataResponseSerializer.defaultEmptyResponseCodes,
emptyRequestMethods: Set<HTTPMethod> = DataResponseSerializer.defaultEmptyRequestMethods) -> DataResponsePublisher<Data>
| queue |
DispatchQueue on which the DataResponse will be published. .main by default.
|
| preprocessor |
DataPreprocessor which filters the Data before serialization. PassthroughPreprocessor() by default.
|
| emptyResponseCodes |
Set<Int> of HTTP status codes for which empty responses are allowed. [204, 205] by default.
|
| emptyRequestMethods |
Set<HTTPMethod> of HTTPMethods for which empty responses are allowed, regardless of status code. [.head] by default.
|
The DataResponsePublisher.
`
publishString(queue:preprocessor:encoding:emptyResponseCodes:emptyRequestMethods:)
`
Creates a DataResponsePublisher for this instance and uses a StringResponseSerializer to serialize the response.
Swift
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
public func publishString(queue: DispatchQueue = .main,
preprocessor: any DataPreprocessor = StringResponseSerializer.defaultDataPreprocessor,
encoding: String.Encoding? = nil,
emptyResponseCodes: Set<Int> = StringResponseSerializer.defaultEmptyResponseCodes,
emptyRequestMethods: Set<HTTPMethod> = StringResponseSerializer.defaultEmptyRequestMethods) -> DataResponsePublisher<String>
| queue |
DispatchQueue on which the DataResponse will be published. .main by default.
|
| preprocessor |
DataPreprocessor which filters the Data before serialization. PassthroughPreprocessor() by default.
|
| encoding |
String.Encoding to parse the response. nil by default, in which case the encoding will be determined by the server response, falling back to the default HTTP character set, ISO-8859-1.
|
| emptyResponseCodes |
Set<Int> of HTTP status codes for which empty responses are allowed. [204, 205] by default.
|
| emptyRequestMethods |
Set<HTTPMethod> of HTTPMethods for which empty responses are allowed, regardless of status code. [.head] by default.
|
The DataResponsePublisher.
`
publishDecodable(type:queue:preprocessor:decoder:emptyResponseCodes:emptyResponseMethods:)
`
Undocumented
Swift
@_disfavoredOverload
public func publishDecodable<T: Decodable>(type: T.Type = T.self,
queue: DispatchQueue = .main,
preprocessor: any DataPreprocessor = DecodableResponseSerializer<T>.defaultDataPreprocessor,
decoder: any DataDecoder = JSONDecoder(),
emptyResponseCodes: Set<Int> = DecodableResponseSerializer<T>.defaultEmptyResponseCodes,
emptyResponseMethods: Set<HTTPMethod> = DecodableResponseSerializer<T>.defaultEmptyRequestMethods) -> DataResponsePublisher<T>
`
publishDecodable(type:queue:preprocessor:decoder:emptyResponseCodes:emptyRequestMethods:)
`
Creates a DataResponsePublisher for this instance and uses a DecodableResponseSerializer to serialize the response.
Swift
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
public func publishDecodable<T: Decodable>(type: T.Type = T.self,
queue: DispatchQueue = .main,
preprocessor: any DataPreprocessor = DecodableResponseSerializer<T>.defaultDataPreprocessor,
decoder: any DataDecoder = JSONDecoder(),
emptyResponseCodes: Set<Int> = DecodableResponseSerializer<T>.defaultEmptyResponseCodes,
emptyRequestMethods: Set<HTTPMethod> = DecodableResponseSerializer<T>.defaultEmptyRequestMethods) -> DataResponsePublisher<T>
| type |
Decodable type to which to decode response Data. Inferred from the context by default.
|
| queue |
DispatchQueue on which the DataResponse will be published. .main by default.
|
| preprocessor |
DataPreprocessor which filters the Data before serialization. PassthroughPreprocessor() by default.
|
| decoder |
DataDecoder instance used to decode response Data. JSONDecoder() by default.
|
| emptyResponseCodes |
Set<Int> of HTTP status codes for which empty responses are allowed. [204, 205] by default.
|
| emptyRequestMethods |
Set<HTTPMethod> of HTTPMethods for which empty responses are allowed, regardless of status code. [.head] by default.
|
The DataResponsePublisher.
`
publishUnserialized(queue:)
`
Creates a DataResponsePublisher for this instance which does not serialize the response before publishing.
DispatchQueue on which the DataResponse will be published. .main by default.Swift
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
public func publishUnserialized(queue: DispatchQueue = .main) -> DataResponsePublisher<Data?>
The DataResponsePublisher.
`
httpResponses(bufferingPolicy:)
`
Creates a StreamOf<HTTPURLResponse> for the instance’s responses.
Swift
public func httpResponses(bufferingPolicy: StreamOf<HTTPURLResponse>.BufferingPolicy = .unbounded) -> StreamOf<HTTPURLResponse>
| bufferingPolicy |
BufferingPolicy that determines the stream’s buffering behavior..unbounded by default.
|
The StreamOf<HTTPURLResponse>.
`
onHTTPResponse(perform:)
`
Sets an async closure returning a Request.ResponseDisposition, called whenever the DataRequest produces an HTTPURLResponse.
Note
Most requests will only produce a single response for each outgoing attempt (initial + retries). However, some types of response may trigger multiple HTTPURLResponses, such as multipart streams, where responses after the first will contain the part headers.
Swift
@discardableResult
@_disfavoredOverload
public func onHTTPResponse(
perform handler: @escaping @Sendable (_ response: HTTPURLResponse) async -> ResponseDisposition
) -> Self
| handler |
Async closure executed when a new HTTPURLResponse is received and returning a ResponseDisposition value. This value determines whether to continue the request or cancel it as if cancel() had been called on the instance. Note, this closure is called on an arbitrary thread, so any synchronous calls in it will execute in that context.
|
The instance.
`
onHTTPResponse(perform:)
`
Sets an async closure called whenever the DataRequest produces an HTTPURLResponse.
Note
Most requests will only produce a single response for each outgoing attempt (initial + retries). However, some types of response may trigger multiple HTTPURLResponses, such as multipart streams, where responses after the first will contain the part headers.
Swift
@discardableResult
public func onHTTPResponse(perform handler: @escaping @Sendable (_ response: HTTPURLResponse) async -> Void) -> Self
| handler |
Async closure executed when a new HTTPURLResponse is received. Note, this closure is called on an arbitrary thread, so any synchronous calls in it will execute in that context.
|
The instance.
`
serializingData(automaticallyCancelling:dataPreprocessor:emptyResponseCodes:emptyRequestMethods:)
`
Creates a DataTask to await a Data value.
Swift
public func serializingData(automaticallyCancelling shouldAutomaticallyCancel: Bool = true,
dataPreprocessor: any DataPreprocessor = DataResponseSerializer.defaultDataPreprocessor,
emptyResponseCodes: Set<Int> = DataResponseSerializer.defaultEmptyResponseCodes,
emptyRequestMethods: Set<HTTPMethod> = DataResponseSerializer.defaultEmptyRequestMethods) -> DataTask<Data>
| shouldAutomaticallyCancel |
Bool determining whether or not the request should be cancelled when the enclosing async context is cancelled. Only applies to DataTask‘s async properties. true by default.
|
| dataPreprocessor |
DataPreprocessor which processes the received Data before completion.
|
| emptyResponseCodes |
HTTP response codes for which empty responses are allowed. [204, 205] by default.
|
| emptyRequestMethods |
HTTPMethods for which empty responses are always valid. [.head] by default.
|
The DataTask.
`
serializingDecodable(_:automaticallyCancelling:dataPreprocessor:decoder:emptyResponseCodes:emptyRequestMethods:)
`
Creates a DataTask to await serialization of a Decodable value.
Swift
public func serializingDecodable<Value: Decodable>(_ type: Value.Type = Value.self,
automaticallyCancelling shouldAutomaticallyCancel: Bool = true,
dataPreprocessor: any DataPreprocessor = DecodableResponseSerializer<Value>.defaultDataPreprocessor,
decoder: any DataDecoder = JSONDecoder(),
emptyResponseCodes: Set<Int> = DecodableResponseSerializer<Value>.defaultEmptyResponseCodes,
emptyRequestMethods: Set<HTTPMethod> = DecodableResponseSerializer<Value>.defaultEmptyRequestMethods) -> DataTask<Value>
| type |
Decodable type to decode from response data.
|
| shouldAutomaticallyCancel |
Bool determining whether or not the request should be cancelled when the enclosing async context is cancelled. Only applies to DataTask‘s async properties. true by default.
|
| dataPreprocessor |
DataPreprocessor which processes the received Data before calling the serializer. PassthroughPreprocessor() by default.
|
| decoder |
DataDecoder to use to decode the response. JSONDecoder() by default.
|
| emptyResponseCodes |
HTTP status codes for which empty responses are always valid. [204, 205] by default.
|
| emptyRequestMethods |
HTTPMethods for which empty responses are always valid. [.head] by default.
|
The DataTask.
`
serializingString(automaticallyCancelling:dataPreprocessor:encoding:emptyResponseCodes:emptyRequestMethods:)
`
Creates a DataTask to await serialization of a String value.
Swift
public func serializingString(automaticallyCancelling shouldAutomaticallyCancel: Bool = true,
dataPreprocessor: any DataPreprocessor = StringResponseSerializer.defaultDataPreprocessor,
encoding: String.Encoding? = nil,
emptyResponseCodes: Set<Int> = StringResponseSerializer.defaultEmptyResponseCodes,
emptyRequestMethods: Set<HTTPMethod> = StringResponseSerializer.defaultEmptyRequestMethods) -> DataTask<String>
| shouldAutomaticallyCancel |
Bool determining whether or not the request should be cancelled when the enclosing async context is cancelled. Only applies to DataTask‘s async properties. true by default.
|
| dataPreprocessor |
DataPreprocessor which processes the received Data before calling the serializer. PassthroughPreprocessor() by default.
|
| encoding |
String.Encoding to use during serialization. Defaults to nil, in which case the encoding will be determined from the server response, falling back to the default HTTP character set, ISO-8859-1.
|
| emptyResponseCodes |
HTTP status codes for which empty responses are always valid. [204, 205] by default.
|
| emptyRequestMethods |
HTTPMethods for which empty responses are always valid. [.head] by default.
|
The DataTask.
`
serializingResponse(using:automaticallyCancelling:)
`
Creates a DataTask to await serialization using the provided ResponseSerializer instance.
Swift
public func serializingResponse<Serializer: ResponseSerializer>(using serializer: Serializer,
automaticallyCancelling shouldAutomaticallyCancel: Bool = true)
-> DataTask<Serializer.SerializedObject>
| serializer |
ResponseSerializer responsible for serializing the request, response, and data.
|
| shouldAutomaticallyCancel |
Bool determining whether or not the request should be cancelled when the enclosing async context is cancelled. Only applies to DataTask‘s async properties. true by default.
|
The DataTask.
`
serializingResponse(using:automaticallyCancelling:)
`
Creates a DataTask to await serialization using the provided DataResponseSerializerProtocol instance.
Swift
public func serializingResponse<Serializer: DataResponseSerializerProtocol>(using serializer: Serializer,
automaticallyCancelling shouldAutomaticallyCancel: Bool = true)
-> DataTask<Serializer.SerializedObject>
| serializer |
DataResponseSerializerProtocol responsible for serializing the request, response, and data.
|
| shouldAutomaticallyCancel |
Bool determining whether or not the request should be cancelled when the enclosing async context is cancelled. Only applies to DataTask‘s async properties. true by default.
|
The DataTask.
`
Validation
`
A closure used to validate a request that takes a URL request, a URL response and data, and returns whether the request was valid.
Swift
public typealias Validation = @Sendable (URLRequest?, HTTPURLResponse, Data?) -> ValidationResult
`
validate(statusCode:)
`
Validates that the response has a status code in the specified sequence.
If validation fails, subsequent calls to response handlers will have an associated error.
Swift
@discardableResult
@preconcurrency
public func validate<S>(statusCode acceptableStatusCodes: S) -> Self where S : Sendable, S : Sequence, S.Element == Int
| acceptableStatusCodes |
Sequence of acceptable response status codes.
|
The instance.
`
validate(contentType:)
`
Validates that the response has a content type in the specified sequence.
If validation fails, subsequent calls to response handlers will have an associated error.
Swift
@discardableResult
@preconcurrency
public func validate<S>(contentType acceptableContentTypes: @escaping @Sendable @autoclosure () -> S) -> Self where S : Sendable, S : Sequence, S.Element == String
| contentType |
The acceptable content types, which may specify wildcard types and/or subtypes.
|
The request.
`
validate()
`
Validates that the response has a status code in the default acceptable range of 200…299, and that the content type matches any specified in the Accept HTTP header field.
If validation fails, subsequent calls to response handlers will have an associated error.
Swift
@discardableResult
public func validate() -> Self
The request.