Sample code for 30+ languages & platforms
Lua

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

Lua
local chilkat = require("chilkat")

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

local 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.accept = "application/dns-json"

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

local jsonResp = http:quick_get_str(url)
if not http.last_method_success then
    print(http.last_error_text)
    return
end

print("Response Status Code: " .. http.last_status)

local json = chilkat.JsonObject.new()
json:load(jsonResp)
json.emit_compact = false
print(json:emit())

if http.last_status ~= 200 then
    print("Failed.")
    return
end

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

local name = ""
local type_ = 0
local TTL = 0
local data = ""
local ipAddr = ""

local Status = json:int_of("Status")
local TC = json:bool_of("TC")
local RD = json:bool_of("RD")
local RA = json:bool_of("RA")
local AD = json:bool_of("AD")
local CD = json:bool_of("CD")
local i = 0
local count_i = json:size_of_array("Question")
while i < count_i do
    json.i = i
    name = json:string_of("Question[i].name")
    type_ = json:int_of("Question[i].type")
    i = i + 1
end

i = 0
count_i = json:size_of_array("Answer")
-- The domain name resolves to 1 or more IP addresses..
while i < count_i do
    json.i = i
    name = json:string_of("Answer[i].name")
    type_ = json:int_of("Answer[i].type")
    TTL = json:int_of("Answer[i].TTL")
    ipAddr = json:string_of("Answer[i].data")

    print("IP Address: " .. ipAddr)
    i = i + 1
end