ClientRequest

public class ClientRequest

This class provides a set of low level APIs for issuing HTTP requests to another server. A new instance of the request can be created, along with options if the user would like to specify certain parameters such as HTTP headers, HTTP methods, host names, and SSL credentials. Data and String objects can be added to a ClientRequest too, and URLs can be parsed.

Usage Example:

//Function to create a new `ClientRequest` using a URL.
 public static func request(_ url: String, callback: @escaping ClientRequest.Callback) -> ClientRequest {
     return ClientRequest(url: url, callback: callback)
 }

 //Create a new `ClientRequest` using a URL.
 let request = HTTP.request("http://localhost/8080") {response in
     ...
 }
  • The set of HTTP headers to be sent with the request.

    Usage Example:

    clientRequest.headers["Content-Type"] = ["text/plain"]
    

    Declaration

    Swift

    public var headers: [String : String]
  • url

    The URL for the request.

    Usage Example:

    clientRequest.url = "https://localhost:8080"
    

    Declaration

    Swift

    public private(set) var url: String { get }
  • The HTTP method (i.e. GET, POST, PUT, DELETE) for the request.

    Usage Example:

    clientRequest.method = "post"
    

    Declaration

    Swift

    public private(set) var method: String { get }
  • The username to be used if using Basic Auth authentication.

    Usage Example:

    clientRequest.userName = "user1"
    

    Declaration

    Swift

    public private(set) var userName: String? { get }
  • The password to be used if using Basic Auth authentication.

    Usage Example:

    clientRequest.password = "sUpeR_seCurE_paSsw0rd"
    

    Declaration

    Swift

    public private(set) var password: String? { get }
  • The maximum number of redirects before failure.

    Note

    The ClientRequest class will automatically follow redirect responses. To avoid redirect loops, it will at maximum follow maxRedirects redirects.

    Usage Example:

    clientRequest.maxRedirects = 10
    

    Declaration

    Swift

    public private(set) var maxRedirects: Int { get }
  • If true, the “Connection: close” header will be added to the request that is sent.

    Usage Example:

    ClientRequest.closeConnection = false
    

    Declaration

    Swift

    public private(set) var closeConnection: Bool { get }
  • Client request options enum. This allows the client to specify certain parameteres such as HTTP headers, HTTP methods, host names, and SSL credentials.

    Usage Example:

    //If present in the options provided, the client will try to use HTTP/2 protocol for the connection.
    Options.useHTTP2
    
    See more

    Declaration

    Swift

    public enum Options
  • Response callback closure type.

    Usage Example:

    var ClientRequest.headers["Content-Type"] = ["text/plain"]
    

    Declaration

    Swift

    public typealias Callback = (ClientResponse?) -> Void

    Parameters

    ClientResponse

    The ClientResponse object that describes the response that was received from the remote server.

  • Set a single option in the request. URL parameters must be set in init().

    Usage Example:

    var options: [ClientRequest.Options] = []
    options.append(.port(Int16(port)))
    clientRequest.set(options)
    

    Declaration

    Swift

    public func set(_ option: Options)

    Parameters

    option

    An Options instance describing the change to be made to the request.

  • Parse an URL (String) into an array of ClientRequest.Options.

    Usage Example:

    let url: String = "http://www.website.com"
    let parsedOptions = clientRequest.parse(url)
    

    Declaration

    Swift

    public class func parse(_ urlString: String) -> [ClientRequest.Options]

    Parameters

    urlString

    A String object referencing a URL.

    Return Value

    An array of ClientRequest.Options

  • Parse an URL Foudation object into an array of ClientRequest.Options.

    Usage Example:

    let url: URL = URL(string: "http://www.website.com")!
    let parsedOptions = clientRequest.parse(url)
    

    Declaration

    Swift

    public class func parse(_ url: URL) -> [ClientRequest.Options]

    Parameters

    url

    Foundation URL object.

    Return Value

    An array of ClientRequest.Options

  • Add a String to the body of the request to be sent.

    Usage Example:

    let stringToSend: String = "send something"
    clientRequest.write(from: stringToSend)
    

    Declaration

    Swift

    public func write(from string: String)

    Parameters

    from

    The String to be added to the request.

  • Add the bytes in a Data struct to the body of the request to be sent.

    Usage Example:

    let string = "some some more stuff"
    if let data: Data = string.data(using: .utf8) {
       clientRequest.write(from: data)
    }
    
    

    Declaration

    Swift

    public func write(from data: Data)

    Parameters

    from

    The Data Struct containing the bytes to be added to the request.

  • Add a String to the body of the request to be sent and then send the request to the remote server.

    Usage Example:

    let data: String = "send something"
    clientRequest.end(from: data, close: true)
    

    Declaration

    Swift

    public func end(_ data: String, close: Bool = false)

    Parameters

    data

    The String to be added to the request.

    close

    If true, add the “Connection: close” header to the set of headers sent with the request.

  • Add the bytes in a Data struct to the body of the request to be sent and then send the request to the remote server.

    Usage Example:

    let stringToSend = "send this"
    let data: Data = stringToSend.data(using: .utf8) {
       clientRequest.end(from: data, close: true)
    }
    

    Declaration

    Swift

    public func end(_ data: Data, close: Bool = false)

    Parameters

    data

    The Data struct containing the bytes to be added to the request.

    close

    If true, add the “Connection: close” header to the set of headers sent with the request.

  • Send the request to the remote server.

    Usage Example:

    clientRequest.end(true)
    

    Declaration

    Swift

    public func end(close: Bool = false)

    Parameters

    close

    If true, add the “Connection: close” header to the set of headers sent with the request.