Skip to content

zig-toolbelt/zio

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Zio logo

Zig Tests License



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:

  • Client with base_url support for relative URL resolution.
  • Full HTTP method support: GET, POST, PUT, PATCH, DELETE, HEAD.
  • Custom request headers via RequestOptions.
  • Response with status, body, and response headers (getHeader(name)).
  • Proper memory management (init / deinit(allocator)).

Installation

  1. Run zig fetch to add the dependency:
zig fetch --save https://github.com/etroynov/zio/archive/refs/tags/0.2.1.tar.gz
  1. In build.zig import the module:
const zio_dep = b.dependency("zio", .{
    .target = target,
    .optimize = optimize,
});
exe.root_module.addImport("zio", zio_dep.module("zio"));

Quick Start

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

API

const zio = @import("zio");

Client

// 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.

Response

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)

Contributing

Contributions are welcome! Please:

  1. Fork the repo.
  2. Create your feature branch (git checkout -b feature/foo).
  3. Commit changes (git commit -am 'Add some foo').
  4. Push to branch (git push origin feature/foo).
  5. Create Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file.

About

Zio is a powerful HTTP client for Zig, inspired by Dio (Dart). Supports global settings, interceptors, FormData, request aborting and timeouts, file uploading/downloading, custom adapters, and much more.

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages