Request

public class Request

Responsible for sending a request and receiving the response and associated data from the server, as well as managing its underlying NSURLSessionTask.

  • The delegate for the underlying task.

    Declaration

    Swift

    public let delegate: TaskDelegate
  • The underlying task.

    Declaration

    Swift

    public var task: NSURLSessionTask { return delegate.task }
  • The session belonging to the underlying task.

    Declaration

    Swift

    public let session: NSURLSession
  • The request sent or to be sent to the server.

    Declaration

    Swift

    public var request: NSURLRequest? { return task.originalRequest }
  • The response received from the server, if any.

    Declaration

    Swift

    public var response: NSHTTPURLResponse? { return task.response as? NSHTTPURLResponse }
  • The progress of the request lifecycle.

    Declaration

    Swift

    public var progress: NSProgress { return delegate.progress }
  • Associates an HTTP Basic credential with the request.

    Declaration

    Swift

    public func authenticate(
            user user: String,
            password: String,
            persistence: NSURLCredentialPersistence = .ForSession)
            -> Self

    Parameters

    user

    The user.

    password

    The password.

    persistence

    The URL credential persistence. .ForSession by default.

    Return Value

    The request.

  • Associates a specified credential with the request.

    Declaration

    Swift

    public func authenticate(usingCredential credential: NSURLCredential) -> Self

    Parameters

    credential

    The credential.

    Return Value

    The request.

  • Returns a base64 encoded basic authentication credential as an authorization header dictionary.

    Declaration

    Swift

    public static func authorizationHeader(user user: String, password: String) -> [String: String]

    Parameters

    user

    The user.

    password

    The password.

    Return Value

    A dictionary with Authorization key and credential value or empty dictionary if encoding fails.

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

    • For uploads, the progress closure returns the bytes written, total bytes written, and total bytes expected to write.
    • For downloads and data tasks, the progress closure returns the bytes read, total bytes read, and total bytes expected to read.

    Declaration

    Swift

    public func progress(closure: ((Int64, Int64, Int64) -> Void)? = nil) -> Self

    Parameters

    closure

    The code to be executed periodically during the lifecycle of the request.

    Return Value

    The request.

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

    This closure returns the bytes most recently received from the server, not including data from previous calls. If this closure is set, data will only be available within this closure, and will not be saved elsewhere. It is also important to note that the response closure will be called with nil responseData.

    Declaration

    Swift

    public func stream(closure: (NSData -> Void)? = nil) -> Self

    Parameters

    closure

    The code to be executed periodically during the lifecycle of the request.

    Return Value

    The request.

  • Resumes the request.

    Declaration

    Swift

    public func resume()
  • Suspends the request.

    Declaration

    Swift

    public func suspend()
  • Cancels the request.

    Declaration

    Swift

    public func cancel()
  • The task delegate is responsible for handling all delegate callbacks for the underlying task as well as executing all operations attached to the serial operation queue upon task completion.

    See more

    Declaration

    Swift

    public class TaskDelegate: NSObject
  • Creates a download file destination closure which uses the default file manager to move the temporary file to a file URL in the first available directory with the specified search path directory and search path domain mask.

    Declaration

    Swift

    public class func suggestedDownloadDestination(
            directory directory: NSSearchPathDirectory = .DocumentDirectory,
            domain: NSSearchPathDomainMask = .UserDomainMask)
            -> DownloadFileDestination

    Parameters

    directory

    The search path directory. .DocumentDirectory by default.

    domain

    The search path domain mask. .UserDomainMask by default.

    Return Value

    A download file destination closure.

  • The resume data of the underlying download task if available after a failure.

    Declaration

    Swift

    public var resumeData: NSData?
  • The textual representation used when written to an output stream, which includes the HTTP method and URL, as well as the response status code if a response has been received.

    Declaration

    Swift

    public var description: String
  • The textual representation used when written to an output stream, in the form of a cURL command.

    Declaration

    Swift

    public var debugDescription: String
  • Adds a handler to be called once the request has finished.

    Declaration

    Swift

    public func response(
            queue queue: dispatch_queue_t? = nil,
            completionHandler: (NSURLRequest?, NSHTTPURLResponse?, NSData?, NSError?) -> Void)
            -> Self

    Parameters

    queue

    The queue on which the completion handler is dispatched.

    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

    public func response<T: ResponseSerializerType>(
            queue queue: dispatch_queue_t? = nil,
            responseSerializer: T,
            completionHandler: Response<T.SerializedObject, T.ErrorObject> -> Void)
            -> Self

    Parameters

    queue

    The queue on which the completion handler is dispatched.

    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.

  • Creates a response serializer that returns the associated data as-is.

    Declaration

    Swift

    public static func dataResponseSerializer() -> ResponseSerializer<NSData, NSError>

    Return Value

    A data response serializer.

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

    Declaration

    Swift

    public func responseData(
            queue queue: dispatch_queue_t? = nil,
            completionHandler: Response<NSData, NSError> -> Void)
            -> Self

    Parameters

    completionHandler

    The code to be executed once the request has finished.

    Return Value

    The request.

  • Creates a response serializer that returns a string initialized from the response data with the specified string encoding.

    Declaration

    Swift

    public static func stringResponseSerializer(
            encoding encoding: NSStringEncoding? = nil)
            -> ResponseSerializer<String, NSError>

    Parameters

    encoding

    The string encoding. If nil, the string encoding will be determined from the server response, falling back to the default HTTP default character set, ISO-8859-1.

    Return Value

    A string response serializer.

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

    Declaration

    Swift

    public func responseString(
            queue queue: dispatch_queue_t? = nil,
            encoding: NSStringEncoding? = nil,
            completionHandler: Response<String, NSError> -> Void)
            -> Self

    Parameters

    encoding

    The string encoding. If nil, the string encoding will be determined from the server response, falling back to the default HTTP default character set, ISO-8859-1.

    completionHandler

    A closure to be executed once the request has finished.

    Return Value

    The request.

  • Creates a response serializer that returns a JSON object constructed from the response data using NSJSONSerialization with the specified reading options.

    Declaration

    Swift

    public static func JSONResponseSerializer(
            options options: NSJSONReadingOptions = .AllowFragments)
            -> ResponseSerializer<AnyObject, NSError>

    Parameters

    options

    The JSON serialization reading options. .AllowFragments by default.

    Return Value

    A JSON object response serializer.

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

    Declaration

    Swift

    public func responseJSON(
            queue queue: dispatch_queue_t? = nil,
            options: NSJSONReadingOptions = .AllowFragments,
            completionHandler: Response<AnyObject, NSError> -> Void)
            -> Self

    Parameters

    options

    The JSON serialization reading options. .AllowFragments by default.

    completionHandler

    A closure to be executed once the request has finished.

    Return Value

    The request.

  • Creates a response serializer that returns an object constructed from the response data using NSPropertyListSerialization with the specified reading options.

    Declaration

    Swift

    public static func propertyListResponseSerializer(
            options options: NSPropertyListReadOptions = NSPropertyListReadOptions())
            -> ResponseSerializer<AnyObject, NSError>

    Parameters

    options

    The property list reading options. NSPropertyListReadOptions() by default.

    Return Value

    A property list object response serializer.

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

    Declaration

    Swift

    public func responsePropertyList(
            queue queue: dispatch_queue_t? = nil,
            options: NSPropertyListReadOptions = NSPropertyListReadOptions(),
            completionHandler: Response<AnyObject, NSError> -> Void)
            -> Self

    Parameters

    options

    The property list reading options. 0 by default.

    completionHandler

    A closure to be executed once the request has finished. The closure takes 3 arguments: the URL request, the URL response, the server data and the result produced while creating the property list.

    Return Value

    The request.

  • Used to represent whether validation was successful or encountered an error resulting in a failure.

    • Success: The validation was successful.
    • Failure: The validation failed encountering the provided error.
    See more

    Declaration

    Swift

    public enum ValidationResult
  • Validates the request, using the specified closure.

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

    Declaration

    Swift

    public func validate(validation: Validation) -> Self

    Parameters

    validation

    A closure to validate the request.

    Return Value

    The request.

  • Validates that the response has a status code in the specified range.

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

    Declaration

    Swift

    public func validate<S: SequenceType where S.Generator.Element == Int>(statusCode acceptableStatusCode: S) -> Self

    Parameters

    range

    The range of acceptable status codes.

    Return Value

    The request.

  • Validates that the response has a content type in the specified array.

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

    Declaration

    Swift

    public func validate<S : SequenceType where S.Generator.Element == String>(contentType acceptableContentTypes: S) -> Self

    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

    public func validate() -> Self

    Return Value

    The request.