Sample code for 30+ languages & platforms
Dart

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

Dart
import 'package:chilkat/chilkat.dart';

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

  final http = CkHttp();

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

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

  final String jsonResp;
  try {
    jsonResp = http.quickGetStr(url);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  print('Response Status Code: ${http.lastStatus}');

  final json = CkJsonObject();
  json.load(jsonResp);
  json.emitCompact = false;
  print(json.emit());

  if (http.lastStatus != 200) {
    print('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

  var i = 0;
  var countI = 0;
  var name = '';
  var type = 0;
  var ttl = 0;
  final data = '';
  var ipAddr = '';

  final status = json.intOf('Status');
  i = 0;
  countI = json.sizeOfArray('Question');
  while (i < countI) {
    json.i = i;
    name = json.stringOf('Question[i].name');
    type = 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');
    type = json.intOf('Answer[i].type');
    ttl = json.intOf('Answer[i].TTL');
    ipAddr = json.stringOf('Answer[i].data');

    print('IP Address: $ipAddr');
    i++;
  }
}