Sample code for 30+ languages & platforms
Xbase++

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 Xbase++ Downloads

Xbase++
LOCAL oHttp
LOCAL cUrl
LOCAL cJsonResp
LOCAL oJson
LOCAL nStatus
LOCAL nTC
LOCAL nRD
LOCAL nRA
LOCAL nAD
LOCAL nCD
LOCAL i
LOCAL nCount_i
LOCAL cName
LOCAL nType
LOCAL nTTL
LOCAL cData
LOCAL cIpAddr

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

oHttp := CreateObject("Chilkat.Http")

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

oHttp:Accept := "application/dns-json"

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

cJsonResp := oHttp:QuickGetStr(cUrl)
IF (oHttp:LastMethodSuccess != 1)
    ? oHttp:LastErrorText
    oHttp:destroy()
    RETURN
ENDIF

? "Response Status Code: " + Str(oHttp:LastStatus)

oJson := CreateObject("Chilkat.JsonObject")
oJson:Load(cJsonResp)
oJson:EmitCompact := 0
? oJson:Emit()

IF (oHttp:LastStatus != 200)
    ? "Failed."
    oHttp:destroy()
    oJson:destroy()
    RETURN
ENDIF

//  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

nStatus := oJson:IntOf("Status")
nTC := oJson:BoolOf("TC")
nRD := oJson:BoolOf("RD")
nRA := oJson:BoolOf("RA")
nAD := oJson:BoolOf("AD")
nCD := oJson:BoolOf("CD")
i := 0
nCount_i := oJson:SizeOfArray("Question")
DO WHILE i < nCount_i
    oJson:I := i
    cName := oJson:StringOf("Question[i].name")
    nType := oJson:IntOf("Question[i].type")
    i := i + 1
ENDDO
i := 0
nCount_i := oJson:SizeOfArray("Answer")
//  The domain name resolves to 1 or more IP addresses..
DO WHILE i < nCount_i
    oJson:I := i
    cName := oJson:StringOf("Answer[i].name")
    nType := oJson:IntOf("Answer[i].type")
    nTTL := oJson:IntOf("Answer[i].TTL")
    cIpAddr := oJson:StringOf("Answer[i].data")

    ? "IP Address: " + cIpAddr
    i := i + 1
ENDDO

oHttp:destroy()
oJson:destroy()