Zig
Zig
QR Code Generator via api.qrserver.com REST API
See more HTTP Examples
Demonstrates how to generate a QR code using the api.qrserver.com REST API service.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 requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
const http = try chilkat.Http.init();
defer http.deinit();
// Send the following GET request to get a binary response.
// The body of the binary response contains the image data for the QR code.
// https://api.qrserver.com/v1/create-qr-code/?data=[URL-encoded-text]&size=[pixels]x[pixels]
const query_params = try chilkat.JsonObject.init();
defer query_params.deinit();
query_params.updateString("data", "Hello World") catch {};
query_params.updateString("size", "100x100") catch {};
// Get a PNG file..
// Possible formats are:
// png
// gif
// jpeg
// jpg
// svg
// eps
// Case matters. Use lowercase.
query_params.updateString("format", "png") catch {};
// Send the GET request to an endpoint.
// Chilkat will add the url-encoded query params passed in the JSON.
const resp = try chilkat.HttpResponse.init();
defer resp.deinit();
http.httpParams("GET", "https://api.qrserver.com/v1/create-qr-code/", query_params, resp) catch {
std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
return;
};
// Did it succeed?
if (resp.getStatusCode() != 200) {
// The response body, if anything, would not be the image data, but would likely be the error text (or HTML, or whatever...)
std.debug.print("{s}\n", .{try resp.getBodyStr(alloc)});
std.debug.print("Response status = {d}\n", .{resp.getStatusCode()});
std.debug.print("Failed.\n", .{});
return;
}
// Success if we get here..
// Save the binary body as the PNG file, or you can get the bytes of the PNG..
try resp.saveBodyBinary("c:/temp/qa_output/qr_code.png");
// Or get the bytes:
const bd = try chilkat.BinData.init();
defer bd.deinit();
resp.getBodyBd(bd) catch {};
// Use the bytes in bd...
// See the online reference documentation for the function to access the bytes directly.
std.debug.print("Success.\n", .{});
}