Sample code for 30+ languages & platforms
Tcl

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 Tcl Downloads

Tcl

load ./chilkat.dll

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

set http [new_CkHttp]

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

CkHttp_put_Accept $http "application/dns-json"

set url "https://cloudflare-dns.com/dns-query?name=chilkat.io&type=A"

set jsonResp [CkHttp_quickGetStr $http $url]
if {[CkHttp_get_LastMethodSuccess $http] != 1} then {
    puts [CkHttp_lastErrorText $http]
    delete_CkHttp $http
    exit
}

puts "Response Status Code: [CkHttp_get_LastStatus $http]"

set json [new_CkJsonObject]

CkJsonObject_Load $json $jsonResp
CkJsonObject_put_EmitCompact $json 0
puts [CkJsonObject_emit $json]

if {[CkHttp_get_LastStatus $http] != 200} then {
    puts "Failed."
    delete_CkHttp $http
    delete_CkJsonObject $json
    exit
}

# 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

set Status [CkJsonObject_IntOf $json "Status"]
set TC [CkJsonObject_BoolOf $json "TC"]
set RD [CkJsonObject_BoolOf $json "RD"]
set RA [CkJsonObject_BoolOf $json "RA"]
set AD [CkJsonObject_BoolOf $json "AD"]
set CD [CkJsonObject_BoolOf $json "CD"]
set i 0
set count_i [CkJsonObject_SizeOfArray $json "Question"]
while {$i < $count_i} {
    CkJsonObject_put_I $json $i
    set name [CkJsonObject_stringOf $json "Question[i].name"]
    set type [CkJsonObject_IntOf $json "Question[i].type"]
    set i [expr $i + 1]
}
set i 0
set count_i [CkJsonObject_SizeOfArray $json "Answer"]
# The domain name resolves to 1 or more IP addresses..
while {$i < $count_i} {
    CkJsonObject_put_I $json $i
    set name [CkJsonObject_stringOf $json "Answer[i].name"]
    set type [CkJsonObject_IntOf $json "Answer[i].type"]
    set TTL [CkJsonObject_IntOf $json "Answer[i].TTL"]
    set ipAddr [CkJsonObject_stringOf $json "Answer[i].data"]

    puts "IP Address: $ipAddr"
    set i [expr $i + 1]
}

delete_CkHttp $http
delete_CkJsonObject $json