Zio is a minimal HTTP client library for Zig, inspired by Dio (Dart). Built on top of std.http.Client with zero external dependencies.
Features:
Clientwithbase_urlsupport for relative URL resolution.- Full HTTP method support:
GET,POST,PUT,PATCH,DELETE,HEAD. - Custom request headers via
RequestOptions. Responsewithstatus,body, and response headers (getHeader(name)).- Proper memory management (
init/deinit(allocator)).
- Run
zig fetchto add the dependency:
zig fetch --save https://github.com/etroynov/zio/archive/refs/tags/0.2.1.tar.gz- In
build.zigimport the module:
const zio_dep = b.dependency("zio", .{
.target = target,
.optimize = optimize,
});
exe.root_module.addImport("zio", zio_dep.module("zio"));const std = @import("std");
const zio = @import("zio");
pub fn main() !void {
var gpa: std.heap.GeneralPurposeAllocator(.{}) = .init;
defer _ = gpa.deinit();
const allocator = gpa.allocator();
var client = zio.Client.init(allocator, .{
.base_url = "https://httpbin.org",
});
defer client.deinit();
const response = try client.get("/get?a=1", .{
.headers = &.{
.{ .name = "Accept", .value = "application/json" },
},
});
defer response.deinit(allocator);
std.debug.print("Status: {}\n", .{response.status});
std.debug.print("Body: {s}\n", .{response.body});
std.debug.print("Content-Type: {s}\n", .{response.getHeader("Content-Type") orelse "n/a"});
}Run with: zig build run
const zio = @import("zio");// Init
var client = zio.Client.init(allocator, .{ .base_url = "https://api.example.com" });
defer client.deinit();
// Methods
const res = try client.get("/path", .{});
const res = try client.post("/path", "body", .{});
const res = try client.put("/path", "body", .{});
const res = try client.patch("/path", "body", .{});
const res = try client.delete("/path", .{});
const res = try client.head("/path", .{});
defer res.deinit(allocator);
// With request headers
const res = try client.get("/path", .{
.headers = &.{
.{ .name = "Authorization", .value = "Bearer token" },
.{ .name = "Accept", .value = "application/json" },
},
});base_url is optional. If path starts with http:// or https://, it is used as-is.
res.status // std.http.Status
res.body // []const u8
res.headers // []const std.http.Header
res.getHeader("Content-Type") // ?[]const u8 — case-insensitive lookup
res.deinit(allocator)Contributions are welcome! Please:
- Fork the repo.
- Create your feature branch (
git checkout -b feature/foo). - Commit changes (
git commit -am 'Add some foo'). - Push to branch (
git push origin feature/foo). - Create Pull Request.
This project is licensed under the MIT License - see the LICENSE file.