Top Related Projects
Ergonomic and modular web framework built with Tokio, Tower, and Hyper
Actix Web is a powerful, pragmatic, and extremely fast web framework for Rust.
An easy and powerful Rust HTTP Client
async fn(Request) -> Result<Response, Error>
Fast and friendly HTTP server framework for async Rust
Quick Overview
Hyper is a fast and modern HTTP server framework for Rust. It provides a high-performance, asynchronous, and extensible platform for building web applications and services.
Pros
- High Performance: Hyper is built on top of the Tokio asynchronous runtime, which allows it to handle a large number of concurrent connections efficiently.
- Extensibility: Hyper is designed to be highly modular, with a plugin system that allows developers to easily add new functionality to the server.
- Asynchronous: Hyper uses asynchronous programming to provide a non-blocking, event-driven architecture, which helps to improve overall performance.
- Rust-based: Hyper is written in Rust, a systems programming language that provides strong guarantees around memory safety and concurrency, making it a great choice for building high-performance web applications.
Cons
- Steep Learning Curve: Rust's syntax and programming model can be challenging for developers who are more familiar with other languages, which may make it harder to get started with Hyper.
- Limited Documentation: While the Hyper project has good documentation, the broader Rust ecosystem can sometimes lack comprehensive documentation, which can make it harder for new users to get up to speed.
- Ecosystem Maturity: Compared to more established web frameworks in other languages, the Rust ecosystem is still relatively young, which means that the availability of third-party libraries and tools may be more limited.
- Performance Overhead: While Hyper is designed to be highly performant, the overhead of the Rust compiler and runtime may be higher than some other web frameworks, especially for simple use cases.
Code Examples
Here are a few examples of how to use Hyper:
use hyper::{Body, Request, Response, Server};
use hyper::service::{make_service_fn, service_fn};
use std::convert::Infallible;
#[tokio::main]
async fn main() {
let addr = "127.0.0.1:3000".parse().unwrap();
let make_svc = make_service_fn(|_conn| async {
Ok::<_, Infallible>(service_fn(|req| async {
Ok::<_, Infallible>(
Response::new(Body::from("Hello, World!"))
)
}))
});
let server = Server::bind(&addr).serve(make_svc);
println!("Listening on http://{}", addr);
if let Err(e) = server.await {
eprintln!("server error: {}", e);
}
}
This example sets up a simple HTTP server that responds with "Hello, World!" to all requests.
use hyper::{Body, Request, Response, Server};
use hyper::service::{make_service_fn, service_fn};
use std::convert::Infallible;
#[tokio::main]
async fn main() {
let addr = "127.0.0.1:3000".parse().unwrap();
let make_svc = make_service_fn(|_conn| async {
Ok::<_, Infallible>(service_fn(|req| async {
let path = req.uri().path();
let response = match path {
"/" => Response::new(Body::from("Home page")),
"/about" => Response::new(Body::from("About page")),
_ => Response::builder()
.status(404)
.body(Body::from("Not found"))
.unwrap(),
};
Ok::<_, Infallible>(response)
}))
});
let server = Server::bind(&addr).serve(make_svc);
println!("Listening on http://{}", addr);
if let Err(e) = server.await {
eprintln!("server error: {}", e);
}
}
This example demonstrates how to handle different routes and return different responses based on the requested path.
use hyper::{Body, Request, Response, Server};
use hyper::service::{make_service_fn, service_fn};
use std::convert::Infall
Competitor Comparisons
Ergonomic and modular web framework built with Tokio, Tower, and Hyper
Pros of Axum
- Higher-level abstractions for building web applications
- Built-in routing system with type-safe path parameters
- Middleware support with easy composition
Cons of Axum
- Less flexible for low-level HTTP handling
- Steeper learning curve for developers new to Tokio ecosystem
- Smaller community and ecosystem compared to Hyper
Code Comparison
Axum example:
async fn handler() -> &'static str {
"Hello, World!"
}
let app = Router::new().route("/", get(handler));
axum::Server::bind(&"0.0.0.0:3000".parse().unwrap())
.serve(app.into_make_service())
.await
.unwrap();
Hyper example:
async fn hello_world(_req: Request<Body>) -> Result<Response<Body>, Infallible> {
Ok(Response::new("Hello, World!".into()))
}
let make_svc = make_service_fn(|_conn| async {
Ok::<_, Infallible>(service_fn(hello_world))
});
Server::bind(&([127, 0, 0, 1], 3000).into())
.serve(make_svc)
.await?;
Axum provides a more concise and higher-level API for building web applications, while Hyper offers more flexibility for low-level HTTP handling. Axum is built on top of Hyper and provides additional abstractions, making it easier to create web services quickly. However, Hyper's lower-level approach allows for more fine-grained control over the HTTP stack when needed.
Actix Web is a powerful, pragmatic, and extremely fast web framework for Rust.
Pros of actix-web
- Higher-level framework with built-in routing and middleware support
- Excellent performance, often topping web framework benchmarks
- Designed for asynchronous programming, leveraging Rust's async/await syntax
Cons of actix-web
- Steeper learning curve due to more complex architecture
- Less flexibility for low-level customization compared to Hyper
- Larger dependency footprint
Code Comparison
actix-web:
use actix_web::{web, App, HttpServer, Responder};
async fn hello() -> impl Responder {
"Hello, World!"
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
App::new().route("/", web::get().to(hello))
})
.bind("127.0.0.1:8080")?
.run()
.await
}
Hyper:
use hyper::{Body, Request, Response, Server};
use hyper::service::{make_service_fn, service_fn};
async fn hello(_: Request<Body>) -> Result<Response<Body>, hyper::Error> {
Ok(Response::new(Body::from("Hello, World!")))
}
#[tokio::main]
async fn main() {
let make_svc = make_service_fn(|_conn| async {
Ok::<_, hyper::Error>(service_fn(hello))
});
let addr = ([127, 0, 0, 1], 8080).into();
let server = Server::bind(&addr).serve(make_svc);
server.await.unwrap();
}
An easy and powerful Rust HTTP Client
Pros of reqwest
- Higher-level API, easier to use for common HTTP client tasks
- Built-in support for async/await
- Automatic handling of redirects, cookies, and authentication
Cons of reqwest
- Less flexible for advanced use cases
- Slightly larger dependency footprint
- May have slightly higher overhead for simple requests
Code Comparison
reqwest:
let res = reqwest::get("https://example.com").await?;
let body = res.text().await?;
hyper:
let client = Client::new();
let res = client.get("https://example.com").await?;
let body = hyper::body::to_bytes(res.into_body()).await?;
Summary
reqwest is built on top of hyper and provides a more user-friendly API for common HTTP client tasks. It's ideal for developers who want a quick and easy way to make HTTP requests without dealing with lower-level details. hyper, on the other hand, offers more flexibility and control, making it suitable for advanced use cases or when building custom HTTP clients. The choice between the two depends on the specific requirements of your project and your familiarity with Rust's async ecosystem.
async fn(Request) -> Result<Response, Error>
Pros of Tower
- More modular and composable architecture, allowing for easier customization and extension of middleware
- Provides a broader set of utilities and abstractions for building robust, asynchronous services
- Better support for service discovery and load balancing
Cons of Tower
- Steeper learning curve due to its more abstract nature
- Less focused on HTTP specifically, which may require additional setup for HTTP-centric applications
- Smaller community and ecosystem compared to Hyper
Code Comparison
Tower service implementation:
struct MyService;
impl Service<Request> for MyService {
type Response = Response;
type Error = Error;
type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>>>>;
fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
fn call(&mut self, req: Request) -> Self::Future {
// Handle request
}
}
Hyper service implementation:
async fn handle(_req: Request<Body>) -> Result<Response<Body>, Infallible> {
Ok(Response::new(Body::from("Hello, World!")))
}
let make_svc = make_service_fn(|_conn| async {
Ok::<_, Infallible>(service_fn(handle))
});
Both Tower and Hyper are powerful libraries for building network services in Rust. Tower offers more flexibility and abstraction, while Hyper provides a more straightforward approach for HTTP-specific applications. The choice between them depends on the project's requirements and complexity.
Fast and friendly HTTP server framework for async Rust
Pros of Tide
- Higher-level abstraction, making it easier to build web applications quickly
- Built-in middleware system for common tasks like logging and compression
- Async-first design, leveraging Rust's async/await syntax
Cons of Tide
- Less flexible for low-level customization compared to Hyper
- Smaller ecosystem and community support
- May have slightly higher overhead due to its higher-level abstractions
Code Comparison
Tide example:
async fn hello(req: Request<()>) -> Result {
Ok(format!("Hello, {}!", req.param("name").unwrap_or("World")).into())
}
#[async_std::main]
async fn main() -> Result<()> {
let mut app = tide::new();
app.at("/hello/:name").get(hello);
app.listen("127.0.0.1:8080").await?;
Ok(())
}
Hyper example:
async fn hello(req: Request<Body>) -> Result<Response<Body>> {
let name = req.uri().path().strip_prefix("/hello/").unwrap_or("World");
Ok(Response::new(Body::from(format!("Hello, {}!", name))))
}
#[tokio::main]
async fn main() {
let make_svc = make_service_fn(|_conn| async { Ok::<_, Infallible>(service_fn(hello)) });
let addr = SocketAddr::from(([127, 0, 0, 1], 8080));
Server::bind(&addr).serve(make_svc).await.unwrap();
}
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
hyper
A protective and efficient HTTP library for all.
- HTTP/1 and HTTP/2
- Asynchronous design
- Leading in performance
- Tested and correct
- Extensive production use
- Client and Server APIs
Get started by looking over the guides.
"Low-level"
hyper is a relatively low-level library, meant to be a building block for libraries and applications.
If you are looking for a convenient HTTP client, then you may wish to consider reqwest.
If you are not sure what HTTP server to choose, then you may want to consider axum or warp, the latter taking a more functional approach. Both are built on top of this library.
Contributing
To get involved, take a look at CONTRIBUTING.
If you prefer chatting, there is an active community in the Discord server.
License
hyper is provided under the MIT license. See LICENSE.
Top Related Projects
Ergonomic and modular web framework built with Tokio, Tower, and Hyper
Actix Web is a powerful, pragmatic, and extremely fast web framework for Rust.
An easy and powerful Rust HTTP Client
async fn(Request) -> Result<Response, Error>
Fast and friendly HTTP server framework for async Rust
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot