HTTPClient

public final class HTTPClient

Connects to remote HTTP servers allowing you to send HTTPRequests and receive HTTPResponses.

let httpRes = HTTPClient.connect(hostname: "vapor.codes", on: ...).map(to: HTTPResponse.self) { client in
    return client.send(...)
}
  • Creates a new HTTPClient connected over TCP or TLS.

    let httpRes = HTTPClient.connect(hostname: "vapor.codes", on: ...).map(to: HTTPResponse.self) { client in
        return client.send(...)
    }
    

    Declaration

    Swift

    public static func connect(
        scheme: HTTPScheme = .http,
        hostname: String,
        port: Int? = nil,
        connectTimeout: TimeAmount = TimeAmount.seconds(10),
        on worker: Worker,
        onError: @escaping (Error) -> () = { _ in }
    ) -> Future<HTTPClient>

    Parameters

    scheme

    Transport layer security to use, either tls or plainText.

    hostname

    Remote server’s hostname.

    port

    Remote server’s port, defaults to 80 for TCP and 443 for TLS.

    connectTimeout

    The timeout that will apply to the connection attempt.

    worker

    Worker to perform async work on.

    onError

    Optional closure, which fires when a networking error is caught.

    Return Value

    A Future containing the connected HTTPClient.

  • A Future that will complete when this HTTPClient closes.

    Declaration

    Swift

    public var onClose: Future<Void> { get }
  • Sends an HTTPRequest to the connected, remote server.

    let httpRes = HTTPClient.connect(hostname: "vapor.codes", on: req).map(to: HTTPResponse.self) { client in
        return client.send(...)
    }
    

    Declaration

    Swift

    public func send(_ request: HTTPRequest) -> Future<HTTPResponse>

    Parameters

    request

    HTTPRequest to send to the remote server.

    Return Value

    A Future HTTPResponse containing the server’s response.

  • Closes this HTTPClient‘s connection to the remote server.

    Declaration

    Swift

    public func close() -> Future<Void>
  • Performs an HTTP protocol upgrade connected using the HTTPClient.

    let webSocketUpgrader: ...
    let webSocket = try HTTPClient.upgrade(hostname: "vapor.codes", upgrader: webSocketUpgrader, on: req).wait()
    

    Declaration

    Swift

    public static func upgrade<Upgrader>(
        scheme: HTTPScheme = .http,
        hostname: String,
        port: Int? = nil,
        upgrader: Upgrader,
        on worker: Worker
    ) -> Future<Upgrader.UpgradeResult> where Upgrader: HTTPClientProtocolUpgrader

    Parameters

    scheme

    Transport layer security to use, either http or https.

    hostname

    Remote server’s hostname.

    port

    Remote server’s port, defaults to 80 for TCP and 443 for TLS.

    worker

    Worker to perform async work on.

    Return Value

    A Future containing the upgrade result.