Sample code for 30+ languages & platforms
Rust

Cloudfare DNS over HTTPS

See more Cloudfare Examples

Cloudflare offers a DNS over HTTPS resolver at: https://cloudflare-dns.com/dns-query

This example demonstrates how to do a DNS lookup for a domain using Cloudfare's HTTPS resolver.

Chilkat Rust Downloads

Rust
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

let http = chilkat::Http::new();

// Send the following CURL request:   curl -H 'accept: application/dns-json' 'https://cloudflare-dns.com/dns-query?name=chilkat.io&type=A'

http.set_accept("application/dns-json");

let url = "https://cloudflare-dns.com/dns-query?name=chilkat.io&type=A".to_string();

let Ok(json_resp) = http.quick_get_str(&url) else {
    println!("{}", http.last_error_text());
    return;
};

println!("Response Status Code: {}", http.last_status());

let json = chilkat::JsonObject::new();
let _ = json.load(&json_resp);
json.set_emit_compact(false);
println!("{}", json.emit().unwrap_or_default());

if http.last_status() != 200 {
    println!("Failed.");
    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

let mut i: i32 = 0;
let mut count_i: i32 = 0;
let mut ip_addr = String::new();

let status = json.int_of("Status");
let tc = json.bool_of("TC");
let rd = json.bool_of("RD");
let ra = json.bool_of("RA");
let ad = json.bool_of("AD");
let cd = json.bool_of("CD");
i = 0;
count_i = json.size_of_array("Question");
while i < count_i {
    json.set_i(i);
    let _ = json.string_of("Question[i].name").unwrap_or_default();
    let _ = json.int_of("Question[i].type");
    i = i + 1;
}

i = 0;
count_i = json.size_of_array("Answer");
// The domain name resolves to 1 or more IP addresses..
while i < count_i {
    json.set_i(i);
    let _ = json.string_of("Answer[i].name").unwrap_or_default();
    let _ = json.int_of("Answer[i].type");
    let _ = json.int_of("Answer[i].TTL");
    ip_addr = json.string_of("Answer[i].data").unwrap_or_default();

    println!("IP Address: {}", ip_addr);
    i = i + 1;
}