Client

public protocol Client

Connects to remote HTTP servers and sends HTTP requests receiving HTTP responses.

let res = try req.client().get("http://vapor.codes")
print(res) // Future<Response>
  • get(_:headers:beforeSend:) Extension method

    Sends an HTTP GET Request to a server with an optional configuration closure that will run before sending.

    let res = try client.get("http://api.vapor.codes/users")
    print(res) // Future<Response>
    

    HTTP GET requests are typically used for fetching information and do not have bodies. However, the beforeSend closure is a great place for encoding query string parameters.

    let res = try client.get("http://api.vapor.codes/users") { get in
        try get.query.encode(["name": "vapor"])
    }
    print(res) // Future<Response>
    

    Declaration

    Swift

    public func get(_ url: URLRepresentable, headers: HTTPHeaders = [:], beforeSend: (Request) throws -> () = { _ in }) -> Future<Response>

    Parameters

    url

    Something URLRepresentable that will be converted to a URL. This URL should contain a scheme, hostname, and port.

    headers

    HTTPHeaders to add to the request. Empty by default.

    beforeSend

    An optional closure that can mutate the Request before it is sent.

    Return Value

    A Future containing the requested Response or an Error.

  • post(_:headers:beforeSend:) Extension method

    Sends an HTTP POST Request to a server with an optional configuration closure that will run before sending.

    let user: User ...
    let res = try client.post("http://api.vapor.codes/users") { post in
        try post.content.encode(user)
    }
    print(res) // Future<Response>
    

    Declaration

    Swift

    public func post(_ url: URLRepresentable, headers: HTTPHeaders = [:], beforeSend: (Request) throws -> () = { _ in }) -> Future<Response>

    Parameters

    url

    Something URLRepresentable that will be converted to a URL. This URL should contain a scheme, hostname, and port.

    headers

    HTTPHeaders to add to the request. Empty by default.

    beforeSend

    An optional closure that can mutate the Request before it is sent.

    Return Value

    A Future containing the requested Response or an Error.

  • patch(_:headers:beforeSend:) Extension method

    Sends an HTTP PATCH Request to a server with an optional configuration closure that will run before sending.

    let user: User ...
    let res = try client.patch("http://api.vapor.codes/users/42") { patch in
        try patch.content.encode(user)
    }
    print(res) // Future<Response>
    

    Declaration

    Swift

    public func patch(_ url: URLRepresentable, headers: HTTPHeaders = [:], beforeSend: (Request) throws -> () = { _ in }) -> Future<Response>

    Parameters

    url

    Something URLRepresentable that will be converted to a URL. This URL should contain a scheme, hostname, and port.

    headers

    HTTPHeaders to add to the request. Empty by default.

    beforeSend

    An optional closure that can mutate the Request before it is sent.

    Return Value

    A Future containing the requested Response or an Error.

  • put(_:headers:beforeSend:) Extension method

    Sends an HTTP PUT Request to a server with an optional configuration closure that will run before sending.

    let user: User ...
    let res = try client.put("http://api.vapor.codes/users/42") { put in
        try put.content.encode(user)
    }
    print(res) // Future<Response>
    

    Declaration

    Swift

    public func put(_ url: URLRepresentable, headers: HTTPHeaders = [:], beforeSend: (Request) throws -> () = { _ in }) -> Future<Response>

    Parameters

    url

    Something URLRepresentable that will be converted to a URL. This URL should contain a scheme, hostname, and port.

    headers

    HTTPHeaders to add to the request. Empty by default.

    beforeSend

    An optional closure that can mutate the Request before it is sent.

    Return Value

    A Future containing the requested Response or an Error.

  • Sends an HTTP DELETE Request to a server with an optional configuration closure that will run before sending.

    let res = try client.delete("http://api.vapor.codes/users/42")
    print(res) // Future<Response>
    

    HTTP DELETE requests are typically used for deleting information and do not have bodies. However, the beforeSend closure is a great place for encoding query string parameters.

    let res = try client.delete("http://api.vapor.codes/users") { get in
        try get.query.encode(["name": "vapor"])
    }
    print(res) // Future<Response>
    

    Declaration

    Swift

    public func delete(_ url: URLRepresentable, headers: HTTPHeaders = [:], beforeSend: (Request) throws -> () = { _ in }) -> Future<Response>

    Parameters

    url

    Something URLRepresentable that will be converted to a URL. This URL should contain a scheme, hostname, and port.

    headers

    HTTPHeaders to add to the request. Empty by default.

    beforeSend

    An optional closure that can mutate the Request before it is sent.

    Return Value

    A Future containing the requested Response or an Error.

  • Sends an HTTP Request to a server with an optional configuration closure that will run before sending.

    let user: User ...
    let res = try client.send(.POST, to: "http://api.vapor.codes/users") { post in
        try post.content.encode(user)
    }
    print(res) // Future<Response>
    

    Declaration

    Swift

    public func send(_ method: HTTPMethod, headers: HTTPHeaders = [:], to url: URLRepresentable, beforeSend: (Request) throws -> () = { _ in }) -> Future<Response>

    Parameters

    method

    HTTPMethod to use for the request.

    headers

    HTTPHeaders to add to the request. Empty by default.

    url

    Something URLRepresentable that will be converted to a URL. This URL should contain a scheme, hostname, and port.

    beforeSend

    An optional closure that can mutate the Request before it is sent.

    Return Value

    A Future containing the requested Response or an Error.

  • put(_:headers:content:) Extension method

    Sends a PUT request with body

    Declaration

    Swift

    @available(*, deprecated, message: "Use beforeSend closure instead.")
    public func put<C>(_ url: URLRepresentable, headers: HTTPHeaders = .init(), content: C) -> Future<Response> where C: Content
  • post(_:headers:content:) Extension method

    Sends a POST request with body

    Declaration

    Swift

    @available(*, deprecated, message: "Use beforeSend closure instead.")
    public func post<C>(_ url: URLRepresentable, headers: HTTPHeaders = .init(), content: C) -> Future<Response> where C: Content
  • patch(_:headers:content:) Extension method

    Sends a PATCH request with body

    Declaration

    Swift

    @available(*, deprecated, message: "Use beforeSend closure instead.")
    public func patch<C>(_ url: URLRepresentable, headers: HTTPHeaders = .init(), content: C) -> Future<Response> where C: Content
  • Sends an HTTP GET Request to a server with an optional configuration closure that will run before sending. This Request will ask for upgrade to WebSocket protocol.

    let res = try client.webSocket("http://api.vapor.codes/users")
    print(res) // Future<WebSocket>
    

    HTTP GET requests are typically used for fetching information and do not have bodies. However, the beforeSend closure is a great place for encoding query string parameters.

    let res = try client.get("http://api.vapor.codes/users") { get in
        try get.query.encode(["name": "vapor"])
    }
    print(res) // Future<Response>
    

    Declaration

    Swift

    public func webSocket(_ url: URLRepresentable, headers: HTTPHeaders = [:], beforeSend: (Request) throws -> () = { _ in }) -> Future<WebSocket>

    Parameters

    url

    Something URLRepresentable that will be converted to a URL. This URL should contain a scheme, hostname, and port.

    headers

    HTTPHeaders to add to the request. Empty by default.

    beforeSend

    An optional closure that can mutate the Request before it is sent.

    Return Value

    A Future containing the newly connected WebSocket or an Error.