A production-ready, high-performance HTTP client and server library for Zig
π Documentation | API Reference | Quick Start | Contributing
Warning
This project is currently in active development and considered experimental.
APIs are subject to change without notice. Use with caution in production environments.
httpx.zig is a comprehensive, high-performance HTTP library designed for building robust networked applications. It features a modern API with support for all major HTTP versions, connection pooling, and express-style server routing.
βοΈ If you build with httpx.zig, make sure to give it a star! βοΈ
Note
Custom HTTP/2 & HTTP/3 Implementation: Zig's standard library does not provide HTTP/2, HTTP/3, or QUIC support. httpx.zig implements these protocols entirely from scratch, including:
- HPACK header compression (RFC 7541) for HTTP/2
- HTTP/2 stream multiplexing and flow control (RFC 7540)
- QPACK header compression (RFC 9204) for HTTP/3
- QUIC transport framing (RFC 9000) for HTTP/3
β¨ Features (click to expand)
| Feature | Description |
|---|---|
| π Protocol Support | Full support for HTTP/1.0, HTTP/1.1, HTTP/2 (with HPACK), and HTTP/3 (with QPACK/QUIC). |
| π¦ Header Compression | HPACK (RFC 7541) for HTTP/2 and QPACK (RFC 9204) for HTTP/3. |
| π Stream Multiplexing | HTTP/2 stream state machine with flow control and priority handling. |
| π Connection Pooling | Automatic reuse of TCP connections with keep-alive and health checking. |
| π£οΈ Express-style Routing | Intuitive server routing with dynamic path parameters and groups. |
| π¦ Middleware Stack | Built-in middleware for CORS, Logging, Rate Limiting, customized Auth, and more. |
| π¦ Concurrency | Parallel request patterns (race, all, any) and async task execution. |
| π Interceptors | Global hooks to modify requests and responses (e.g., Auth injection). |
| π Smart Retries | Configurable retry policies with exponential backoff. |
| π JSON & HTML | Helpers for easy JSON serialization and HTML response generation. |
| π TLS/SSL | Secure connections via TLS 1.3 support. |
| π Static Files | Efficient static file serving capabilities. |
| π‘οΈ Security | Security headers (Helmet) and safe defaults. |
| π¦ No External Deps | Pure Zig implementation for maximum portability and ease of build. |
π Prerequisites & Supported Platforms (click to expand)
Before using httpx.zig, ensure you have the following:
| Requirement | Version | Notes |
|---|---|---|
| Zig | 0.15.0+ | Download from ziglang.org |
| Operating System | Windows 10+, Linux, macOS, FreeBSD | Cross-platform networking support |
httpx.zig compiles and runs on a wide range of architectures:
| Platform | x86_64 (64-bit) | aarch64 (ARM64) | i386 (32-bit) | arm (32-bit) |
|---|---|---|---|---|
| Linux | β | β | β | β |
| Windows | β | β | β | β |
| macOS | β | β (Apple Silicon) | β | β |
| FreeBSD | β | β | β | β |
Zig makes cross-compilation easy. Build for any target from any host:
# Build for Linux ARM64 from Windows
zig build -Dtarget=aarch64-linux
# Build for Windows from Linux
zig build -Dtarget=x86_64-windows
# Build for macOS Apple Silicon from Linux
zig build -Dtarget=aarch64-macos
# Build for 32-bit Windows
zig build -Dtarget=i386-windowsAdd to your build.zig.zon:
.dependencies = .{
.httpx = .{
.url = "https://github.com/muhammad-fiaz/httpx.zig/archive/refs/heads/main.tar.gz",
},
},Then in your build.zig:
const httpx = b.dependency("httpx", .{
.target = target,
.optimize = optimize,
});
exe.root_module.addImport("httpx", httpx.module("httpx"));const std = @import("std");
const httpx = @import("httpx");
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
// Create client
var client = httpx.Client.init(allocator);
defer client.deinit();
// Simple GET request
var response = try client.get("https://httpbin.org/get", .{});
defer response.deinit();
if (response.ok()) {
std.debug.print("Response: {s}\n", .{response.text() orelse ""});
}
// POST with JSON
var post_response = try client.post("https://httpbin.org/post", .{
.json = "{\"name\": \"John\"}",
});
defer post_response.deinit();
}const std = @import("std");
const httpx = @import("httpx");
fn helloHandler(ctx: *httpx.Context) anyerror!httpx.Response {
return ctx.json(.{ .message = "Hello, World!" });
}
fn htmlHandler(ctx: *httpx.Context) anyerror!httpx.Response {
return ctx.html("<h1>Hello from httpx.zig!</h1>");
}
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
var server = httpx.Server.init(allocator);
defer server.deinit();
// Add middleware
try server.use(httpx.logger());
try server.use(httpx.cors(.{}));
// Register routes
try server.get("/", helloHandler);
try server.get("/page", htmlHandler);
// Start server
try server.listen();
}The examples/ directory contains comprehensive examples for all features:
- Basic:
simple_get.zig,post_json.zig - Advanced Client:
custom_headers.zig,connection_pool.zig,interceptors.zig - Concurrency:
concurrent_requests.zig(Parallel/Race/All patterns) - Server:
simple_server.zig,router_example.zig,static_files.zig - Middleware:
middleware_example.zig - Streaming:
streaming.zig
To run an example:
zig build run-simple_getRun benchmarks:
zig build benchNote: Benchmark results will vary based on hardware and network conditions.
Contributions are welcome! Please:
- Fork the repository
- Create a feature branch
- Add tests for new functionality
- Ensure all tests pass:
zig build test - Submit a pull request
MIT License - see LICENSE for details.