Created
January 20, 2022 22:12
-
-
Save vinikira/fa83de49c6a5e05086690e6d9001bb03 to your computer and use it in GitHub Desktop.
Using CURL with Ziglang
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
set -xe | |
zig build-exe main.zig -O ReleaseSmall --library curl --library c $(pkg-config --cflags libcurl) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const std = @import("std"); | |
const json = @import("json"); | |
const cURL = @cImport({ | |
@cInclude("curl/curl.h"); | |
}); | |
const Response = struct { ip: []u8, country: []u8, cc: []u8 }; | |
pub fn main() !void { | |
var arena_state = std.heap.ArenaAllocator.init(std.heap.c_allocator); | |
defer arena_state.deinit(); | |
const allocator = arena_state.allocator(); | |
if (cURL.curl_global_init(cURL.CURL_GLOBAL_ALL) != cURL.CURLE_OK) | |
return error.CURLGLobalInitFailed; | |
defer cURL.curl_global_cleanup(); | |
const handle = cURL.curl_easy_init() orelse return error.CURLHAndleInitFailed; | |
defer cURL.curl_easy_cleanup(handle); | |
var response_buffer = std.ArrayList(u8).init(allocator); | |
defer response_buffer.deinit(); | |
if (cURL.curl_easy_setopt(handle, cURL.CURLOPT_URL, "https://api.myip.com") != cURL.CURLE_OK) | |
return error.CouldNotSetURL; | |
if (cURL.curl_easy_setopt(handle, cURL.CURLOPT_WRITEFUNCTION, writeToArrayListCallback) != cURL.CURLE_OK) | |
return error.CouldNotSetWriteCallback; | |
if (cURL.curl_easy_setopt(handle, cURL.CURLOPT_WRITEDATA, &response_buffer) != cURL.CURLE_OK) | |
return error.CouldNotSetWriteData; | |
if (cURL.curl_easy_perform(handle) != cURL.CURLE_OK) | |
return error.FailedToPerformRequest; | |
std.debug.print("Got response of {d} bytes\n", .{response_buffer.items.len}); | |
std.debug.print("{s}\n", .{response_buffer.items}); | |
var stream = std.json.TokenStream.init(response_buffer.items); | |
const parsedResponse = try std.json.parse(Response, &stream, .{ .allocator = allocator }); | |
std.debug.print("Response: \n", .{}); | |
std.debug.print(" IP: {s}\n", .{parsedResponse.ip}); | |
std.debug.print(" Country: {s}\n", .{parsedResponse.country}); | |
std.debug.print(" Country Code: {s}\n", .{parsedResponse.cc}); | |
} | |
fn writeToArrayListCallback(data: *anyopaque, size: c_uint, nmemb: c_uint, user_data: *anyopaque) callconv(.C) c_uint { | |
var buffer = @intToPtr(*std.ArrayList(u8), @ptrToInt(user_data)); | |
var typed_data = @intToPtr([*]u8, @ptrToInt(data)); | |
buffer.appendSlice(typed_data[0 .. nmemb * size]) catch return 0; | |
return nmemb * size; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment