Sample code for 30+ languages & platforms
Unicode C

ipapi.co IPv4 Geolocation Lookup

See more Geolocation Examples

Demonstrates how to lookup Geolocation data for an IPv4 address using the ipapi.co REST API.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkHttpW.h>
#include <C_CkJsonObjectW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkHttpW http;
    const wchar_t *jsonStr;
    HCkJsonObjectW json;
    const wchar_t *ip;
    const wchar_t *city;
    const wchar_t *region;
    const wchar_t *region_code;
    const wchar_t *country;
    const wchar_t *country_name;
    const wchar_t *continent_code;
    BOOL in_eu;
    const wchar_t *postal;
    const wchar_t *latitude;
    const wchar_t *longitude;
    const wchar_t *timezone;
    const wchar_t *utc_offset;
    const wchar_t *country_calling_code;
    const wchar_t *currency;
    const wchar_t *languages;
    const wchar_t *asn;
    const wchar_t *org;

    success = FALSE;

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

    http = CkHttpW_Create();

    // Lookup an IPv4 address: 149.250.207.170  (this was a randomly chosen address)
    jsonStr = CkHttpW_quickGetStr(http,L"https://ipapi.co/149.250.207.170/json");
    if (CkHttpW_getLastMethodSuccess(http) == FALSE) {
        wprintf(L"%s\n",CkHttpW_lastErrorText(http));
        CkHttpW_Dispose(http);
        return;
    }

    json = CkJsonObjectW_Create();
    CkJsonObjectW_putEmitCompact(json,FALSE);
    success = CkJsonObjectW_Load(json,jsonStr);

    wprintf(L"%s\n",CkJsonObjectW_emit(json));

    // Sample output:
    // Use this online tool to generate parsing code from sample JSON: 
    // Generate Parsing Code from JSON

    // {
    //   "ip": "149.250.207.170",
    //   "city": "B�blingen",
    //   "region": "Baden-W�rttemberg",
    //   "region_code": null,
    //   "country": "DE",
    //   "country_name": "Germany",
    //   "continent_code": "EU",
    //   "in_eu": true,
    //   "postal": null,
    //   "latitude": null,
    //   "longitude": null,
    //   "timezone": null,
    //   "utc_offset": null,
    //   "country_calling_code": "+49",
    //   "currency": "EUR",
    //   "languages": "de",
    //   "asn": "AS15854",
    //   "org": "EntServ Deutschland GmbH"
    // }

    ip = CkJsonObjectW_stringOf(json,L"ip");
    city = CkJsonObjectW_stringOf(json,L"city");
    region = CkJsonObjectW_stringOf(json,L"region");
    region_code = CkJsonObjectW_stringOf(json,L"region_code");
    country = CkJsonObjectW_stringOf(json,L"country");
    country_name = CkJsonObjectW_stringOf(json,L"country_name");
    continent_code = CkJsonObjectW_stringOf(json,L"continent_code");
    in_eu = CkJsonObjectW_BoolOf(json,L"in_eu");
    postal = CkJsonObjectW_stringOf(json,L"postal");
    latitude = CkJsonObjectW_stringOf(json,L"latitude");
    longitude = CkJsonObjectW_stringOf(json,L"longitude");
    timezone = CkJsonObjectW_stringOf(json,L"timezone");
    utc_offset = CkJsonObjectW_stringOf(json,L"utc_offset");
    country_calling_code = CkJsonObjectW_stringOf(json,L"country_calling_code");
    currency = CkJsonObjectW_stringOf(json,L"currency");
    languages = CkJsonObjectW_stringOf(json,L"languages");
    asn = CkJsonObjectW_stringOf(json,L"asn");
    org = CkJsonObjectW_stringOf(json,L"org");


    CkHttpW_Dispose(http);
    CkJsonObjectW_Dispose(json);

    }