ClientResponse

public class ClientResponse

This class describes the response sent by the remote server to an HTTP request sent using the ClientRequest class. Data or Strings can be read in, and URLs can be parsed.

Usage Example:

//The `ClientResponse` object that describes the response that was received from the remote server is used in a callback.
public typealias Callback = (ClientResponse?) -> Void
  • HTTP Status code

    Usage Example:

    var httpStatusCode = .unknown
    ...
    }
    

    Declaration

    Swift

    public private(set) var httpStatusCode: HTTPStatusCode = .unknown
  • HTTP Method of the incoming message.

    Usage Example:

    self.method = method
    ...
    }
    

    Declaration

    Swift

    public var method: String
  • Major version of HTTP of the response

    Usage Example:

    print(String(describing: request.httpVersionMajor))
    

    Declaration

    Swift

    public var httpVersionMajor: UInt16?
  • Minor version of HTTP of the response

    Usage Example:

    print(String(describing: request.httpVersionMinor))
    

    Declaration

    Swift

    public var httpVersionMinor: UInt16?
  • The set of HTTP headers received with the response

    Usage Example:

    let protocols = request.headers["Upgrade"]
    

    Declaration

    Swift

    public var headers: HeadersContainer
  • Read a chunk of the body of the response.

    Throws

    if an error occurs while reading the body.

    Usage Example:

    let readData = try self.read(into: data)
    

    Declaration

    Swift

    public func read(into data: inout Data) throws -> Int

    Parameters

    into

    An NSMutableData to hold the data in the response.

    Return Value

    the number of bytes read.

  • Read the whole body of the response.

    Throws

    if an error occurs while reading the data.

    Usage Example:

    let length = try request.readAllData(into: &body)
    

    Declaration

    Swift

    public func readAllData(into data: inout Data) throws -> Int

    Parameters

    into

    An NSMutableData to hold the data in the response.

    Return Value

    the number of bytes read.

  • Read a chunk of the body and return it as a String.

    Throws

    if an error occurs while reading the data.

    Usage Example:

    let body = try request.readString()
    

    Declaration

    Swift

    public func readString() throws -> String?

    Return Value

    an Optional string.

  • The HTTP Status code, as an Int, sent in the response by the remote server.

    Usage Example:

    statusCode = HTTPStatusCode(rawValue: status) ?? .unknown
    

    Declaration

    Swift

    public internal(set) var status = -1
  • The HTTP Status code, as an HTTPStatusCode, sent in the response by the remote server.

    Usage Example:

    response.statusCode = .badRequest
    

    Declaration

    Swift

    public internal(set) var statusCode: HTTPStatusCode = HTTPStatusCode.unknown