Zig
Zig
Example: Http.SetCookieXml method
Demonstrates theSetCookieXml method.
Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
const http = try chilkat.Http.init();
defer http.deinit();
http.setSaveCookies(true);
http.setCookieDir("c:/temp/cookie_cache");
// For this initial request, we are not sending cookies.
// We are starting a new (cookie) session and saving the cookies we receive.
http.setSendCookies(false);
// Do a request that establishes and saves cookies in files in the cookie directory.
var html: [:0]const u8 = try http.quickGetStr(alloc, "https://google.com/");
// Examine the LastResponseHeader to see the cookies that were received:
std.debug.print("{s}\n", .{try http.getLastResponseHeader(alloc)});
std.debug.print("----------------------------------\n", .{});
// We can see the following Set-Cookie response header fields:
// Set-Cookie: AEC=AVh_V2h-fsL2-****; expires=Mon, 23-Feb-2026 19:15:10 GMT; path=/; domain=.google.com; Secure; HttpOnly; SameSite=lax
// Set-Cookie: NID=525=XC_cL****Hn7-WONX; expires=Thu, 26-Feb-2026 19:15:10 GMT; path=/; domain=.google.com; HttpOnly
// The following file was created: c:/temp/cookie_cache/google_com.xml
// Examine the cookies received in the above request.
std.debug.print("{s}\n", .{try http.getCookieXml(alloc, "google.com")});
std.debug.print("----------------------------------\n", .{});
// Sample output.
// <?xml version="1.0" encoding="utf-8"?>
// <cookies>
// <cookie key=".google.com,/,AEC" v="0" expire="Mon, 23-Feb-2026 19:05:20 GMT" secure="yes">
// <AEC>AVh_V2gL8QzTdFGYq6_rS6ktBfqm8WNG3pzHxS2nTZD5i23dRBau2c4ZRA</AEC>
// </cookie>
// <cookie key=".google.com,/,NID" v="0" expire="Thu, 26-Feb-2026 19:05:20 GMT">
// <NID>525=SuLcnaSkFqF4Jz_jLEq4kt_f3MY2Xro1VDoVzLKvp8XHcW2UHuLKJSr55iDeW0NiRIPXoAwJWF1-YNl29unX2xfhEWsS5BhbuK_2DXdD9cTOmn5BSENMhZasxeJ71mEP2PQRMXBndqnl41DhblC2jjdac_so4TESIll1B0GCVe9wRFjqI6DTZItRCj61BHmr1_RAQi0_jrh_ihn6KYtIFEY7</NID>
// </cookie>
// </cookies>
// -------------------------------------------------------------------------------------------
// Let's say we want to continue with the session at some later time with a new HTTP object.
// One way to do it is to simply set the same cookie properties:
const http_b = try chilkat.Http.init();
defer http_b.deinit();
http_b.setSaveCookies(true);
http_b.setCookieDir("c:/temp/cookie_cache");
http_b.setSendCookies(true);
// The cookies from the cache are sent.
html = try http_b.quickGetStr(alloc, "https://google.com/");
// Examine the LastHeader to see the contents of the Cookie header field.
std.debug.print("{s}\n", .{try http_b.getLastHeader(alloc)});
std.debug.print("----------------------------------\n", .{});
// -------------------------------------------------------------------------------------------
// Another way to do it is to explicitly load the cookies from XML
// and set the cookies for the particular domain.
const http_c = try chilkat.Http.init();
defer http_c.deinit();
http_c.setSaveCookies(true);
// Do not save cookies to files, just keep them in memory.
http_c.setCookieDir("memory");
http_c.setSendCookies(true);
const sb_cookies_xml = try chilkat.StringBuilder.init();
defer sb_cookies_xml.deinit();
sb_cookies_xml.loadFile("c:/temp/cookie_cache/google_com.xml", "utf-8") catch {
std.debug.print("{s}\n", .{try sb_cookies_xml.getLastErrorText(alloc)});
return;
};
http_c.setCookieXml("google.com", try sb_cookies_xml.getAsString(alloc)) catch {
std.debug.print("{s}\n", .{try http_c.getLastErrorText(alloc)});
return;
};
html = try http_c.quickGetStr(alloc, "https://google.com/");
// Examine the LastHeader to see the contents of the Cookie header field.
std.debug.print("{s}\n", .{try http_c.getLastHeader(alloc)});
}