Zig
Zig
HTTPS GET URL with URL Encoded Query Params
See more HTTP Examples
Creates a URL with URL encoded query params, and sends an HTTPS GET to the URL.Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// Create a URL with URL-encoded query params.
const sb_url = try chilkat.StringBuilder.init();
defer sb_url.deinit();
sb_url.append("https://www.example.com/something?") catch {};
// If we know a query param value does not have any special chars requiring URL encoding,
// just add it. In this case, there is no need to URL encode "123" because "123" url encoded is still just "123".
sb_url.append("abc=123&xyz=") catch {};
const sb_value = try chilkat.StringBuilder.init();
defer sb_value.deinit();
sb_value.append("this is a test") catch {};
sb_url.append("msg=") catch {};
sb_url.append(try sb_value.getEncoded(alloc, "url", "utf-8")) catch {};
// sbUrl contains: https://www.example.com/something?abc=123&xyz=msg=this%20is%20a%20test
std.debug.print("{s}\n", .{try sb_url.getAsString(alloc)});
// Send the GET request and get the response body, which is assumed to be text of some sort (HTML, XML, JSON, etc.)
// (Chilkat automatically decompresses gzipped responses)
const http = try chilkat.Http.init();
defer http.deinit();
const response_body = http.quickGetStr(alloc, try sb_url.getAsString(alloc)) catch {
std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
return;
};
std.debug.print("Response Body:\n", .{});
std.debug.print("{s}\n", .{response_body});
}