Hello world HTTP server with hyper 1.x in Rust

hyper is a low-level HTTP library for Rust with client and server support for HTTP/1 and HTTP/2. Frameworks such as axum are built on it. You’d use hyper directly when you want control over connection handling, or you’re writing a library rather than an app; for most web apps, a framework on top of hyper is less work.
Since 1.0, hyper only provides the protocol implementation. Things like the Tokio integration and convenience body types live in two companion crates: hyper-util and http-body-util.
Dependencies
Section titled “Dependencies”[dependencies]hyper = { version = "1", features = ["full"] }hyper-util = { version = "0.1", features = ["full"] }http-body-util = "0.1"tokio = { version = "1", features = ["full"] }full is the easy option for an example. In a real project you can narrow it down, e.g. hyper = { version = "1", features = ["server", "http1"] } and hyper-util = { version = "0.1", features = ["tokio"] }. This was checked against hyper 1.12 and hyper-util 0.1.21.
The server
Section titled “The server”src/main.rs:
use std::convert::Infallible;use std::net::SocketAddr;
use http_body_util::Full;use hyper::body::{Bytes, Incoming};use hyper::server::conn::http1;use hyper::service::service_fn;use hyper::{Request, Response};use hyper_util::rt::TokioIo;use tokio::net::TcpListener;
async fn handle_request(_req: Request<Incoming>) -> Result<Response<Full<Bytes>>, Infallible> { let response = Response::builder() .status(200) .header("Content-Type", "text/plain") .body(Full::new(Bytes::from("Hello, World!"))) .unwrap();
Ok(response)}
#[tokio::main]async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> { let addr = SocketAddr::from(([127, 0, 0, 1], 8080)); let listener = TcpListener::bind(addr).await?; println!("Listening on http://{addr}");
loop { let (stream, _) = listener.accept().await?; // Adapts a Tokio TcpStream to hyper's own IO traits. let io = TokioIo::new(stream);
// One task per connection so connections are served concurrently. tokio::task::spawn(async move { if let Err(err) = http1::Builder::new() .serve_connection(io, service_fn(handle_request)) .await { eprintln!("Error serving connection: {err:?}"); } }); }}What each piece does:
handle_requestis the service. It takes aRequest<Incoming>(the streaming request body) and returns a response whose body isFull<Bytes>, a body that is sent in one chunk. The.unwrap()on the builder only fails if you pass an invalid header or status, which is a programming error here.TcpListeneraccepts connections. hyper 1.x no longer does this for you; the accept loop is your code.TokioIobridges Tokio’sAsyncRead/AsyncWriteto hyper’s runtime-agnostic IO traits.http1::Builder::serve_connectionruns HTTP/1 on one connection and calls the service for each request on it.
Running it
Section titled “Running it”cargo runcurl -i http://127.0.0.1:8080You should get a 200 OK with Hello, World! as the body.
Gotchas and next steps
Section titled “Gotchas and next steps”- This server only speaks HTTP/1. To accept both HTTP/1 and HTTP/2 on the same port, use
hyper_util::server::conn::auto::Builderin place ofhttp1::Builder. - There is no graceful shutdown: Ctrl+C drops in-flight connections. hyper-util has a
GracefulShutdownhelper for this. - Routing, middleware, and JSON extraction are not hyper’s job. If you need them, use axum (built on hyper) or add
towerservices yourself.
Further reading
Section titled “Further reading”© 2026 David Li · Home · Blog · RSS ·Classic site · Built with Astro, Starlight and three.js · Logos:Font Awesome Free (CC BY 4.0)