Module hyper::client [] [src]

HTTP Client

There are two levels of APIs provided for construct HTTP clients:

  • The higher-level Client type.
  • The lower-level conn module.


The Client is the main way to send HTTP requests to a server. The default Client provides these things on top of the lower-level API:

  • A default connector, able to resolve hostnames and connect to destinations over plain-text TCP.
  • A pool of existing connections, allowing better performance when making multiple requests to the same hostname.
  • Automatic setting of the Host header, based on the request Uri.
  • Automatic request retries when a pooled connection is closed by the server before any bytes have been written.

Many of these features can configured, by making use of Client::builder.


extern crate hyper;

use hyper::Client;
use hyper::rt::{self, lazy, Future, Stream};

fn main() {
    // A runtime is needed to execute our asynchronous code.
    rt::run(lazy(|| {
        let client = Client::new();

            // Make a GET /ip to ''

            // And then, if the request gets a response...
            .and_then(|res| {
                println!("status: {}", res.status());

                // Concatenate the body stream into a single buffer...
                // This returns a new future, since we must stream body.

            // And then, if reading the full body succeeds...
            .and_then(|body| {
                // The body is just bytes, but let's print a string...
                let s = ::std::str::from_utf8(&body)
                    .expect("httpbin sends utf-8 JSON");

                println!("body: {}", s);

                // and_then requires we return a new Future, and it turns
                // out that Result is a Future that is ready immediately.

            // Map any errors that might have happened...
            .map_err(|err| {
                println!("error: {}", err);



Lower-level client connection API.


The Connect trait, and supporting types.



Builder for a Client


A Client to make outgoing HTTP requests.


A connector for the http scheme.


A Future that will resolve to an HTTP Response.