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] -
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
TheClientRequestclass will automatically follow redirect responses. To avoid redirect loops, it will at maximum followmaxRedirectsredirects.Usage Example:
clientRequest.maxRedirects = 10Declaration
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 = falseDeclaration
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:
See more//If present in the options provided, the client will try to use HTTP/2 protocol for the connection. Options.useHTTP2Declaration
Swift
public enum Options -
Response callback closure type.
Usage Example:
var ClientRequest.headers["Content-Type"] = ["text/plain"]Declaration
Swift
public typealias Callback = (ClientResponse?) -> VoidParameters
ClientResponseThe
ClientResponseobject 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
optionAn
Optionsinstance 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
urlStringA 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
urlFoundation 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
fromThe 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
fromThe 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
dataThe String to be added to the request.
closeIf 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
dataThe Data struct containing the bytes to be added to the request.
closeIf 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
closeIf true, add the “Connection: close” header to the set of headers sent with the request.
View on GitHub
ClientRequest Class Reference