Zig
Zig
Cloudfare DNS over HTTPS
See more Cloudfare Examples
Cloudflare offers a DNS over HTTPS resolver at: https://cloudflare-dns.com/dns-queryThis example demonstrates how to do a DNS lookup for a domain using Cloudfare's HTTPS resolver.
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 CURL request: curl -H 'accept: application/dns-json' 'https://cloudflare-dns.com/dns-query?name=chilkat.io&type=A'
http.setAccept("application/dns-json");
const url = "https://cloudflare-dns.com/dns-query?name=chilkat.io&type=A";
const json_resp = http.quickGetStr(alloc, url) catch {
std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
return;
};
std.debug.print("Response Status Code: {d}\n", .{http.getLastStatus()});
const json = try chilkat.JsonObject.init();
defer json.deinit();
json.load(json_resp) catch {};
json.setEmitCompact(false);
std.debug.print("{s}\n", .{try json.emit(alloc)});
if (http.getLastStatus() != 200) {
std.debug.print("Failed.\n", .{});
return;
}
// Sample output...
// (See the parsing code below..)
//
// {
// "Status": 0,
// "TC": false,
// "RD": true,
// "RA": true,
// "AD": false,
// "CD": false,
// "Question": [
// {
// "name": "chilkat.io.",
// "type": 1
// }
// ],
// "Answer": [
// {
// "name": "chilkat.io.",
// "type": 1,
// "TTL": 900,
// "data": "52.25.83.238"
// }
// ]
// }
// Use this online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON
var i: i32 = 0;
var count_i: i32 = 0;
var name: [:0]const u8 = "";
var type_: i32 = 0;
var ttl: i32 = 0;
var ip_addr: [:0]const u8 = "";
_ = json.intOf("Status");
_ = json.boolOf("TC");
_ = json.boolOf("RD");
_ = json.boolOf("RA");
_ = json.boolOf("AD");
_ = json.boolOf("CD");
i = 0;
count_i = json.sizeOfArray("Question");
while (i < count_i) {
json.setI(i);
name = try json.stringOf(alloc, "Question[i].name");
type_ = json.intOf("Question[i].type");
i = i + 1;
}
i = 0;
count_i = json.sizeOfArray("Answer");
// The domain name resolves to 1 or more IP addresses..
while (i < count_i) {
json.setI(i);
name = try json.stringOf(alloc, "Answer[i].name");
type_ = json.intOf("Answer[i].type");
ttl = json.intOf("Answer[i].TTL");
ip_addr = try json.stringOf(alloc, "Answer[i].data");
std.debug.print("IP Address: {s}\n", .{ip_addr});
i = i + 1;
}
}