zig-curl
zig-curl copied to clipboard
Zig bindings for libcurl
#+TITLE: zig-curl #+DATE: 2023-09-16T23:16:15+0800 #+LASTMOD: 2024-03-02T20:57:50+0800 #+OPTIONS: toc:nil num:nil #+STARTUP: content
[[https://github.com/jiacai2050/zig-curl/actions/workflows/CI.yml][https://github.com/jiacai2050/zig-curl/actions/workflows/CI.yml/badge.svg]]
Zig bindings to [[https://curl.haxx.se/libcurl/][libcurl]], a free and easy-to-use client-side URL transfer library.
#+begin_quote This package is in its early stage, although the core functionality works right now, the API is still subject to changes.
=zig-curl= only support [[https://ziglang.org/download/][Zig master]], any contribution is welcome. ⚒️ #+end_quote
The vendored libraries consist of: | Library | Version | |---------+---------| | libcurl | [[https://github.com/curl/curl/tree/curl-8_5_0][8.5.0]] | | zlib | [[https://github.com/madler/zlib/tree/v1.3][1.3]] | | mbedtls | [[https://github.com/Mbed-TLS/mbedtls/tree/v3.5.1][3.5.1]] |
- Usage #+begin_src zig const curl = @import("curl");
pub fn main() !void { var gpa = std.heap.GeneralPurposeAllocator(.{}){}; defer if (gpa.deinit() != .ok) @panic("leak"); const allocator = gpa.allocator();
const easy = try curl.Easy.init(allocator, .{});
defer easy.deinit();
const resp = try easy.get("http://httpbin.org/anything");
defer resp.deinit();
std.debug.print("Status code: {d}\nBody: {s}\n", .{
resp.status_code,
resp.body.items,
});
} #+end_src See [[file:examples/basic.zig]], [[file:examples/advanced.zig]] for more usage.
- Installation =zig-curl= support [[https://ziglang.org/download/0.11.0/release-notes.html#Package-Management][package]] introduced in Zig 0.11.
#+begin_src bash zig fetch --save=curl https://github.com/jiacai2050/zig-curl/archive/${COMMIT}.tar.gz #+end_src
Replace ~${COMMIT}~ with a real one, then in your =build.zig=, import the module like this: #+begin_src zig const dep_curl = b.dependency("curl", .{}); exe.root_module.addImport("curl", dep_curl.module("curl")); exe.linkLibC(); #+end_src
This library will link to a vendored libcurl by default, you can disable it and link to system-wide with this #+begin_src zig const dep_curl = b.dependency("curl", .{ .link_vendor = false }); exe.linkSystemLibrary("curl"); exe.linkLibC(); #+end_src
- Roadmap
- [x] Currently only easy API is supported, support [[https://curl.se/libcurl/c/libcurl-multi.html][multi API]].
- License [[file:LICENSE][MIT]]