1
0
Fork 0
mirror of https://github.com/zigzap/zap.git synced 2025-10-20 23:24:09 +00:00

Fixup examples/cookies

This commit is contained in:
JacobCrabill 2024-01-30 20:58:26 -08:00
parent 7a39a34f58
commit 58352bc4b8

View file

@ -11,12 +11,14 @@ fn makeRequest(a: std.mem.Allocator, url: []const u8) !void {
var http_client: std.http.Client = .{ .allocator = a }; var http_client: std.http.Client = .{ .allocator = a };
defer http_client.deinit(); defer http_client.deinit();
var req = try http_client.request(.GET, uri, h, .{}); try h.append("cookie", "ZIG_ZAP=awesome");
defer req.deinit();
try req.headers.append("cookie", "ZIG_ZAP=awesome"); var req = try http_client.fetch(a, .{
try req.start(); .location = .{ .uri = uri },
try req.wait(); .method = .GET,
.headers = h,
});
defer req.deinit();
} }
fn makeRequestThread(a: std.mem.Allocator, url: []const u8) !std.Thread { fn makeRequestThread(a: std.mem.Allocator, url: []const u8) !std.Thread {
@ -28,7 +30,7 @@ pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{ var gpa = std.heap.GeneralPurposeAllocator(.{
.thread_safe = true, .thread_safe = true,
}){}; }){};
var allocator = gpa.allocator(); const allocator = gpa.allocator();
const Handler = struct { const Handler = struct {
var alloc: std.mem.Allocator = undefined; var alloc: std.mem.Allocator = undefined;
@ -39,7 +41,7 @@ pub fn main() !void {
r.parseCookies(false); r.parseCookies(false);
var cookie_count = r.getCookiesCount(); const cookie_count = r.getCookiesCount();
std.log.info("cookie_count: {}", .{cookie_count}); std.log.info("cookie_count: {}", .{cookie_count});
// iterate over all cookies as strings // iterate over all cookies as strings
@ -52,11 +54,12 @@ pub fn main() !void {
std.debug.print("\n", .{}); std.debug.print("\n", .{});
// // iterate over all cookies // iterate over all cookies
const cookies = r.cookiesToOwnedList(alloc, false) catch unreachable; const cookies = r.cookiesToOwnedList(alloc, false) catch unreachable;
defer cookies.deinit(); defer cookies.deinit();
for (cookies.items) |kv| { for (cookies.items) |kv| {
std.log.info("cookie `{s}` is {any}", .{ kv.key.str, kv.value }); // TODO: compile error when trying to print 'kv.value'
std.log.info("cookie `{s}` is a {any}", .{ kv.key.str, @TypeOf(kv.value) });
} }
// let's get cookie "ZIG_ZAP" by name // let's get cookie "ZIG_ZAP" by name