Sample code for 30+ languages & platforms
PureBasic

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

PureBasic
IncludeFile "CkJsonObject.pb"
IncludeFile "CkHttp.pb"

Procedure ChilkatExample()

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

    http.i = CkHttp::ckCreate()
    If http.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

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

    CkHttp::setCkAccept(http, "application/dns-json")

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

    jsonResp.s = CkHttp::ckQuickGetStr(http,url)
    If CkHttp::ckLastMethodSuccess(http) <> 1
        Debug CkHttp::ckLastErrorText(http)
        CkHttp::ckDispose(http)
        ProcedureReturn
    EndIf

    Debug "Response Status Code: " + Str(CkHttp::ckLastStatus(http))

    json.i = CkJsonObject::ckCreate()
    If json.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckLoad(json,jsonResp)
    CkJsonObject::setCkEmitCompact(json, 0)
    Debug CkJsonObject::ckEmit(json)

    If CkHttp::ckLastStatus(http) <> 200
        Debug "Failed."
        CkHttp::ckDispose(http)
        CkJsonObject::ckDispose(json)
        ProcedureReturn
    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

    Status.i
    TC.i
    RD.i
    RA.i
    AD.i
    CD.i
    i.i
    count_i.i
    name.s
    type.i
    TTL.i
    data.s
    ipAddr.s

    Status = CkJsonObject::ckIntOf(json,"Status")
    TC = CkJsonObject::ckBoolOf(json,"TC")
    RD = CkJsonObject::ckBoolOf(json,"RD")
    RA = CkJsonObject::ckBoolOf(json,"RA")
    AD = CkJsonObject::ckBoolOf(json,"AD")
    CD = CkJsonObject::ckBoolOf(json,"CD")
    i = 0
    count_i = CkJsonObject::ckSizeOfArray(json,"Question")
    While i < count_i
        CkJsonObject::setCkI(json, i)
        name = CkJsonObject::ckStringOf(json,"Question[i].name")
        type = CkJsonObject::ckIntOf(json,"Question[i].type")
        i = i + 1
    Wend
    i = 0
    count_i = CkJsonObject::ckSizeOfArray(json,"Answer")
    ; The domain name resolves to 1 or more IP addresses..
    While i < count_i
        CkJsonObject::setCkI(json, i)
        name = CkJsonObject::ckStringOf(json,"Answer[i].name")
        type = CkJsonObject::ckIntOf(json,"Answer[i].type")
        TTL = CkJsonObject::ckIntOf(json,"Answer[i].TTL")
        ipAddr = CkJsonObject::ckStringOf(json,"Answer[i].data")

        Debug "IP Address: " + ipAddr
        i = i + 1
    Wend


    CkHttp::ckDispose(http)
    CkJsonObject::ckDispose(json)


    ProcedureReturn
EndProcedure