DataRequest

public class DataRequest : Request

Request subclass which handles in-memory Data download using URLSessionDataTask.

  • URLRequestConvertible value used to create URLRequests for this instance.

    Declaration

    Swift

    public let convertible: URLRequestConvertible
  • Data read from the server so far.

    Declaration

    Swift

    public var data: Data? { get }
  • Validates the request, using the specified closure.

    Note

    If validation fails, subsequent calls to response handlers will have an associated error.

    Declaration

    Swift

    @discardableResult
    public func validate(_ validation: @escaping Validation) -> Self

    Parameters

    validation

    Validation closure used to validate the response.

    Return Value

    The instance.

DataRequest / UploadRequest

DataTask

Default

  • Adds a handler to be called once the request has finished.

    Declaration

    Swift

    @discardableResult
    public func response(queue: DispatchQueue = .main, completionHandler: @escaping (AFDataResponse<Data?>) -> Void) -> Self

    Parameters

    queue

    The queue on which the completion handler is dispatched. .main by default.

    completionHandler

    The code to be executed once the request has finished.

    Return Value

    The request.

  • Adds a handler to be called once the request has finished.

    Declaration

    Swift

    @discardableResult
    public func response<Serializer: DataResponseSerializerProtocol>(queue: DispatchQueue = .main,
                                                                     responseSerializer: Serializer,
                                                                     completionHandler: @escaping (AFDataResponse<Serializer.SerializedObject>) -> Void)
        -> Self

    Parameters

    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.

    Return Value

    The request.

  • Adds a handler to be called once the request has finished.

    Declaration

    Swift

    @discardableResult
    public func response<Serializer: ResponseSerializer>(queue: DispatchQueue = .main,
                                                         responseSerializer: Serializer,
                                                         completionHandler: @escaping (AFDataResponse<Serializer.SerializedObject>) -> Void)
        -> Self

    Parameters

    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.

    Return Value

    The request.

Data

String

  • Adds a handler using a StringResponseSerializer to be called once the request has finished.

    Declaration

    Swift

    @discardableResult
    public func responseString(queue: DispatchQueue = .main,
                               dataPreprocessor: DataPreprocessor = StringResponseSerializer.defaultDataPreprocessor,
                               encoding: String.Encoding? = nil,
                               emptyResponseCodes: Set<Int> = StringResponseSerializer.defaultEmptyResponseCodes,
                               emptyRequestMethods: Set<HTTPMethod> = StringResponseSerializer.defaultEmptyRequestMethods,
                               completionHandler: @escaping (AFDataResponse<String>) -> Void) -> Self

    Parameters

    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.

    Return Value

    The request.

JSON

  • Adds a handler using a JSONResponseSerializer to be called once the request has finished.

    Declaration

    Swift

    @available(*, deprecated, message: "responseJSON deprecated and will be removed in Alamofire 6. Use responseDecodable instead.")
    @discardableResult
    public func responseJSON(queue: DispatchQueue = .main,
                             dataPreprocessor: DataPreprocessor = JSONResponseSerializer.defaultDataPreprocessor,
                             emptyResponseCodes: Set<Int> = JSONResponseSerializer.defaultEmptyResponseCodes,
                             emptyRequestMethods: Set<HTTPMethod> = JSONResponseSerializer.defaultEmptyRequestMethods,
                             options: JSONSerialization.ReadingOptions = .allowFragments,
                             completionHandler: @escaping (AFDataResponse<Any>) -> Void) -> Self

    Parameters

    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.

    Return Value

    The request.

Decodable

  • A closure used to validate a request that takes a URL request, a URL response and data, and returns whether the request was valid.

    Declaration

    Swift

    public typealias Validation = (URLRequest?, HTTPURLResponse, Data?) -> ValidationResult
  • 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.

    Declaration

    Swift

    @discardableResult
    public func validate<S>(statusCode acceptableStatusCodes: S) -> Self where S : Sequence, S.Element == Int

    Parameters

    acceptableStatusCodes

    Sequence of acceptable response status codes.

    Return Value

    The instance.

  • 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.

    Declaration

    Swift

    @discardableResult
    public func validate<S>(contentType acceptableContentTypes: @autoclosure @escaping () -> S) -> Self where S : Sequence, S.Element == String

    Parameters

    contentType

    The acceptable content types, which may specify wildcard types and/or subtypes.

    Return Value

    The request.

  • 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.

    Declaration

    Swift

    @discardableResult
    public func validate() -> Self

    Return Value

    The request.