Skip to content

Using Client

Client is a convenience wrapper around the lower level HTTP → Client. It automatically parses things like hostname and port from URIs and helps you encode and decode Content.

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

Container

The first thing you will need is a service Container to create your client.

If you are making this external API request as the result of an incoming request to your server, you should use the Request container to create a client. This is most often the case.

If you need a client during boot, use the Application or if you are in a Command use the command context's container.

Once you have a Container, use the client() method to create a Client.

// Creates a generic Client
let client = try container.client()

Send

Once you have a Client, you can use the send(...) method to send a Request. Note that the request URI must include a scheme and hostname.

let req: Request ...
let res = try client.send(req)
print(res) // Future<Response>

You can also use the convenience methods like get(...), post(...), etc.

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

See Content for more information on encoding and decoding content to messages.

Comments