Sample code for 30+ languages & platforms
React Native

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 React Native Downloads

React Native
import { Http, JsonObject } from '@chilkat/react-native'

async function chilkatExample() {
  // This example requires the Chilkat API to have been previously unlocked.
  // See Global Unlock Sample for sample code.

  const http = new Http();

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

  const url = 'https://cloudflare-dns.com/dns-query?name=chilkat.io&type=A';

  let jsonResp: string;
  try {
    jsonResp = await http.quickGetStrAsync(url);
  } catch {
    console.log(http.lastErrorText);
    return;
  }

  console.log(`Response Status Code: ${http.lastStatus}`);

  const json = new JsonObject();
  json.load(jsonResp);
  json.emitCompact = false;
  console.log(json.emit());

  if (http.lastStatus !== 200) {
    console.log('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 i = 0;
  let countI = 0;
  let name = '';
  let typeArg = 0;
  let ttl = 0;
  const data = '';
  let ipAddr = '';

  const status = json.intOf('Status');
  i = 0;
  countI = json.sizeOfArray('Question');
  while (i < countI) {
    json.i = i;
    name = json.stringOf('Question[i].name');
    typeArg = json.intOf('Question[i].type');
    i++;
  }

  i = 0;
  countI = json.sizeOfArray('Answer');
  // The domain name resolves to 1 or more IP addresses..
  while (i < countI) {
    json.i = i;
    name = json.stringOf('Answer[i].name');
    typeArg = json.intOf('Answer[i].type');
    ttl = json.intOf('Answer[i].TTL');
    ipAddr = json.stringOf('Answer[i].data');

    console.log(`IP Address: ${ipAddr}`);
    i++;
  }
}