HTTP
public class HTTP
A set of helpers for HTTP: status codes mapping, server and client request creation.
Usage Example:
//Create a HTTP server.
let server = HTTP.createServer()
//Create a new a `ClientRequest` instance using a URL.
let request = HTTP.request("http://localhost/8080") {response in
...
}
//Get a `ClientRequest` instance from a URL.
let getHTTP = HTTP.get("http://localhost/8080") { response in
...
}
HTTP.escape(url: testString)
-
Mapping of integer HTTP status codes to the String description.
Usage Example:
var statusText = HTTP.statusCodes[HTTPStatusCode.OK.rawValue]
Declaration
Swift
public static let statusCodes: [Int : String]
-
Declaration
Swift
public static func createServer() -> HTTPServer
Return Value
an instance of
HTTPServer
. -
Create a new
ClientRequest
using URL.Usage Example:
let request = HTTP.request("http://localhost/8080") {response in ... }
Declaration
Swift
public static func request(_ url: String, callback: @escaping ClientRequest.Callback) -> ClientRequest
Parameters
url
URL address for the request.
callback
closure to run after the request.
Return Value
a
ClientRequest
instance -
Create a new
ClientRequest
using a list of options.Usage Example:
let myOptions: [ClientRequest.Options] = [.hostname("localhost"), .port("8080")] let request = HTTP.request(myOptions) { response in // Process the ClientResponse }
Declaration
Swift
public static func request(_ options: [ClientRequest.Options], unixDomainSocketPath: String? = nil, callback: @escaping ClientRequest.Callback) -> ClientRequest
Parameters
options
a list of
ClientRequest.Options
.unixDomainSocketPath
the path of a Unix domain socket that this client should connect to (defaults to
nil
).callback
The closure to run after the request completes. The
ClientResponse?
parameter allows access to the response from the server.Return Value
a
ClientRequest
instance -
Get a
ClientRequest
using URL.Note
This method will invoke the end function of the
ClientRequest
immediately after its creation.Usage Example:
let request = HTTP.get("http://localhost/8080") { response in ... }
Declaration
Swift
public static func get(_ url: String, callback: @escaping ClientRequest.Callback) -> ClientRequest
Parameters
url
URL address for the request.
callback
closure to run after the request.
Return Value
a ClientRequest instance.
-
Transform the URL into escaped characters.
Note
URLs can only be sent over the Internet using the ASCII character set, so character escaping will transform unsafe ASCII characters with a ‘%’ followed by two hexadecimal digits.Usage Example:
HTTP.escape(url: testString)
Declaration
Swift
public static func escape(url: String) -> String