small-http

Crates.iosmall-http
lib.rssmall-http
version0.2.1
created_at2025-02-21 19:34:40.422308+00
updated_at2025-09-11 08:55:45.334241+00
descriptionA simple and small sync HTTP/1.1 server/client library
homepagehttps://github.com/bplaat/crates/tree/master/lib/small-http
repositoryhttps://github.com/bplaat/crates
max_upload_size
id1564621
size80,109
Bastiaan van der Plaat (bplaat)

documentation

README

Small-HTTP Rust library

A simple and small HTTP/1.1 server/client library

Getting Started

A simple example the opens a http server on serves a simple response:

use std::net::{Ipv4Addr, TcpListener};
use small_http::{Request, Response};

fn handler(_req: &Request) -> Response {
    Response::with_body("Hello World!")
}

fn main() {
    let listener = TcpListener::bind((Ipv4Addr::LOCALHOST, 8080))
        .unwrap_or_else(|_| panic!("Can't bind to port"));
    small_http::serve(listener, handler);
}

A simple example the of a http client that fetches a JSON response:

#[derive(serde::Deserialize)]
struct IpInfo {
    hostname: String,
}

fn main() {
    let res = small_http::Request::get("http://ipinfo.io/json")
        .fetch()
        .expect("Can't fetch");
    println!("{}", String::from_utf8_lossy(&res.body));
    let ip_info = res.into_json::<IpInfo>().expect("Can't parse JSON");
    println!("Hostname: {}", ip_info.hostname);
}

See the examples for many more examples.

Important: reduce url dependencies

You can greatly reduce the dependencies of the url crate, by removing the idna support with the following crate update:

cargo update -p idna_adapter --precise 1.0.0

Documentation

See the documentation for more information.

License

Copyright © 2023-2025 Bastiaan van der Plaat

Licensed under the MIT license.

Commit count: 639

cargo fmt