Zig
Zig
Example: Http.SetUrlVar method
Demonstrates the HTTP SetUrlVar 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();
const url = "https://finnhub.io/api/v1/quote?symbol={$symbol}&token={$api_key}";
// When the request is sent, the {$symbol} is replaced with "MSFT"
// and the {$api_key} is replaced with "1234567890ABCDEF"
http.setUrlVar("symbol", "MSFT") catch {};
http.setUrlVar("api_key", "1234567890ABCDEF") catch {};
const sb_json = try chilkat.StringBuilder.init();
defer sb_json.deinit();
http.quickGetSb(url, sb_json) catch {
std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
return;
};
const status_code = http.getLastStatus();
if (status_code != 200) {
std.debug.print("Status code: {d}\n", .{status_code});
std.debug.print("Error Message:\n", .{});
std.debug.print("{s}\n", .{try sb_json.getAsString(alloc)});
} else {
std.debug.print("JSON Stock Quote:\n", .{});
std.debug.print("{s}\n", .{try sb_json.getAsString(alloc)});
}
// Output:
// JSON Stock Quote:
// {"c":522.98,"d":0.5,"dp":0.0957,"h":524.51,"l":520.86,"o":524.28,"pc":522.48,"t":1755271948}
}