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 = [
  • Create a new HTTPServer.

    Usage Example:

    let server = HTTP.createServer()
    

    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 request = HTTP.request([ClientRequest.Options]) {response in
        ...
    }
    

    Declaration

    Swift

    public static func request(_ options: [ClientRequest.Options], callback: @escaping ClientRequest.Callback) -> ClientRequest

    Parameters

    options
    callback

    closure to run after the request.

    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