Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/jiacai2050/zig-curl

Zig bindings for libcurl
https://github.com/jiacai2050/zig-curl

libcurl libcurl-binding libcurl-bindings zig zig-lib zig-package ziglang

Last synced: about 2 months ago
JSON representation

Zig bindings for libcurl

Awesome Lists containing this project

README

        

#+TITLE: zig-curl
#+DATE: 2023-09-16T23:16:15+0800
#+LASTMOD: 2024-05-12T13:20:36+0800
#+OPTIONS: toc:nil num:nil
#+STARTUP: content

[[https://img.shields.io/badge/zig%20version-0.12.0-blue.svg]]
[[https://img.shields.io/badge/zig%20version-master-blue.svg]]
[[https://github.com/jiacai2050/zig-curl/actions/workflows/CI.yml][https://github.com/jiacai2050/zig-curl/actions/workflows/CI.yml/badge.svg]]
[[https://ci.codeberg.org/repos/13257][https://ci.codeberg.org/api/badges/13257/status.svg]]

Zig bindings for [[https://curl.haxx.se/libcurl/][libcurl]], a free and easy-to-use client-side URL transfer library.

#+begin_quote
This package is in early stage, although the core functionality works right now, the API is still subject to changes.
#+end_quote

The vendored libraries consist of:
| Library | Version |
|---------+---------|
| libcurl | [[https://github.com/curl/curl/tree/curl-8_8_0][8.8.0]] |
| zlib | [[https://github.com/madler/zlib/tree/v1.3.1][1.3.1]] |
| mbedtls | [[https://github.com/Mbed-TLS/mbedtls/tree/v3.6.0][3.6.0]] |

* 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
#+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]]