hyper

Fast and safe HTTP for the Rust language.

Get Started

Note: hyper is planning a stable 1.0 release in 2023. These guides will be gradually transitioned to 1.0, but if you're looking for guides for 0.14 you can find them here. If you'd like to learn more about the move to 1.0, this is a good place to start.

hyper is a fast HTTP implementation written in and for Rust.

  • A Client for talking to web services.
  • A Server for building those web services.
  • Blazing fast* thanks to Rust.
  • High concurrency with non-blocking sockets.
  • HTTP/1 and HTTP/2 support.
# extern crate tokio;
# extern crate hyper;
# extern crate http_body_util;
# mod no_run {
use std::{convert::Infallible, net::SocketAddr, error::Error};
use http_body_util::Full;
use hyper::{Request, Response, body::Bytes, service::service_fn};
use hyper::server::conn::http1;
use tokio::net::TcpListener;

async fn hello(
    _: Request<hyper::body::Incoming>,
) -> Result<Response<Full<Bytes>>, Infallible> {
    Ok(Response::new(Full::new(Bytes::from("Hello World!"))))
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
    let addr = SocketAddr::from(([127, 0, 0, 1], 3000));

    let listener = TcpListener::bind(addr).await?;

    loop {
        let (stream, _) = listener.accept().await?;

        tokio::task::spawn(async move {
            if let Err(err) = http1::Builder::new()
                .serve_connection(stream, service_fn(hello))
                .await
            {
                println!("Error serving connection: {:?}", err);
            }
        });
    }
}
# }
# fn main() {}