jules-sommer/zurl
Zig bindings for libcurl, forked from jiacai2050/zig-curl and updated for Zig 0.16.0-dev
refsZig bindings for libcurl, forked from jiacai2050/zig-curl and updated for Zig 0.16.0-dev with additional changes to the API.
The vendored libraries consist of:
| Library | Version |
|---|---|
| libcurl | 8.16.0 |
| zlib | 1.3.1 |
| mbedtls | 3.6.0 |
Example program:
const std = @import("std");
const curl = @import("curl");
const URL = "https://edgebin.liujiacai.net/anything";
pub fn main() !void {
var gpa: std.heap.DebugAllocator(.{}) = .init;
defer if (gpa.deinit() != .ok) @panic("leak");
const allocator = gpa.allocator();
const ca_bundle = try curl.allocCABundle(allocator);
defer ca_bundle.deinit();
const easy = try curl.Easy.init(.{
.ca_bundle = ca_bundle,
});
defer easy.deinit();
{
std.debug.print("GET without body\n", .{});
const resp = try easy.fetch(URL, .{});
std.debug.print("Status code: {d}\n", .{resp.status_code});
}
{
std.debug.print("\nGET with fixed buffer as body\n", .{});
var buffer: [1024]u8 = undefined;
var writer = std.Io.Writer.fixed(&buffer);
const resp = try easy.fetch(URL, .{ .writer = &writer });
std.debug.print("Status code: {d}\nBody: {s}\n", .{ resp.status_code, writer.buffered() });
}
}
Check the examples/ directory for more examples.
Fetch the dependency into your project:
zig fetch --save=zurl git+https://github.com/jules-sommer/zurl
Import the fetched dependency in your build.zig:
const zurl_dep = b.dependency("zurl", .{});
const exe_mod = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
.imports = &[_]std.Build.Module.Import{
.{
.name = "zurl",
.module = zurl.module("zurl"),
},
},
});
By default this library links to the vendored libcurl. To disable vendoring and link against the system libcurl instead:
const zurl_dep = b.dependency("zurl", .{ .link_vendor = false });
const exe_mod = b.createModule(.{
// ... other fields omitted for brevity ...
.link_libc = true,
});
exe_mod.linkSystemLibrary("curl", .{});