Crate http_kit

Source
Expand description

A flexible and ergonomic HTTP toolkit for Rust.

This crate provides high-level abstractions for HTTP operations while maintaining performance and type safety. It’s designed to be no-std compatible with optional standard library features.

§Features

  • Type-safe HTTP primitives - Request, Response, Headers, and Body types with strong type checking
  • Streaming support - Efficient handling of large payloads through streaming interfaces
  • Body transformations - Convert between different body formats (JSON, form data, files) with zero-copy when possible
  • Middleware system - Extensible middleware architecture for request/response processing
  • Async/await ready - Built on top of futures-lite for async I/O operations

§Optional Features

  • json - JSON serialization/deserialization via serde_json (enabled by default)
  • form - Form data handling via serde_urlencoded (enabled by default)
  • fs - File upload support with MIME type detection
  • mime - MIME type parsing and manipulation
  • http_body - Implementation of http_body traits
  • std - Enable standard library support (enabled by default)

§Examples

§Basic Request/Response Handling

use http_kit::{Request, Response, Result};

async fn echo_handler(mut request: Request) -> Result<Response> {
    let body = request.take_body()?;
    Ok(Response::new(200, body))
}

let mut request = Request::get("/echo");
request.replace_body("Hello, world!");
let response = echo_handler(request).await?;

§JSON Handling

use http_kit::{Request, Response, Result};
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize)]
struct User {
    name: String,
    email: String,
}

async fn create_user(mut request: Request) -> Result<Response> {
    let user: User = request.into_json().await?;
    // Process user...
    Ok(Response::empty().json(&user)?)
}

§Middleware Usage

use http_kit::{Request, Response, Result, Middleware, Endpoint};

struct LoggingMiddleware;

impl Middleware for LoggingMiddleware {
    async fn handle(&self, request: &mut Request, next: impl Endpoint) -> Result<Response> {
        println!("Request: {} {}", request.method(), request.uri());
        let response = next.respond(request).await?;
        println!("Response: {}", response.status());
        Ok(response)
    }
}

Re-exports§

pub use cookie;

Modules§

endpoint
HTTP endpoint abstraction for request handling.
header
HTTP header types
method
The HTTP request method
middleware
Middleware functionality for HTTP request and response processing.
uri
URI component of request and response lines
utils
Utility types and functions for HTTP operations.
version
HTTP version

Macros§

msg
Constructs an error with a formatted message and an associated HTTP status code.

Structs§

Body
Flexible HTTP body that can represent data in various forms.
Error
The main error type for HTTP operations.
Extensions
A type map of protocol extensions.
Method
The Request Method (VERB)
Request
An HTTP request with headers, body, and metadata.
Response
An HTTP response with status, headers, and body.
StatusCode
An HTTP status code (status-code in RFC 9110 et al.).
Uri
The URI component of a request.
Version
Represents a version of the HTTP spec.

Enums§

BodyError
Error type for body operations.

Traits§

Endpoint
A trait for types that can handle HTTP requests and generate responses.
Middleware
Trait for implementing middleware that can process HTTP requests and responses.
ResultExt
Extension trait that adds HTTP status code handling to Result and Option types.

Type Aliases§

Result
A specialized Result type for HTTP operations.