Sample code for 30+ languages & platforms
Unicode C

ipinfo.io IPv4 Geolocation Lookup

See more Geolocation Examples

Demonstrates how to lookup Geolocation data for an IPv4 address using the ipinfo.io 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 *hostname;
    const wchar_t *city;
    const wchar_t *region;
    const wchar_t *country;
    const wchar_t *loc;
    const wchar_t *postal;
    const wchar_t *asnAsn;
    const wchar_t *asnName;
    const wchar_t *asnDomain;
    const wchar_t *asnRoute;
    const wchar_t *asnType;
    const wchar_t *companyName;
    const wchar_t *companyDomain;
    const wchar_t *companyType;
    const wchar_t *carrierName;
    const wchar_t *carrierMcc;
    const wchar_t *carrierMnc;

    success = FALSE;

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

    http = CkHttpW_Create();

    CkHttpW_putLogin(http,L"ACCESS_TOKEN");
    CkHttpW_putPassword(http,L"");
    CkHttpW_putAccept(http,L"application/json");

    // Lookup an IPv4 address: 149.250.207.170  (this was a randomly chosen address)
    jsonStr = CkHttpW_quickGetStr(http,L"https://ipinfo.io/149.250.207.170");
    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

    // Note: This is the output for the free trial account.
    // 
    // {
    //   "ip": "149.250.207.170",
    //   "city": "",
    //   "region": "",
    //   "country": "DE",
    //   "loc": "51.2993,9.4910",
    //   "org": "AS15854 EntServ Deutschland GmbH"
    // }

    // A full response would look like this:

    // {
    //   "ip": "66.87.125.72",
    //   "hostname": "66-87-125-72.pools.spcsdns.net",
    //   "city": "Southbridge",
    //   "region": "Massachusetts",
    //   "country": "US",
    //   "loc": "42.0707,-72.0440",
    //   "postal": "01550",
    //   "asn": {
    //     "asn": "AS10507",
    //     "name": "Sprint Personal Communications Systems",
    //     "domain": "spcsdns.net",
    //     "route": "66.87.125.0/24",
    //     "type": "isp"
    //   },
    //   "company": {
    //     "name": "Sprint Springfield POP",
    //     "domain": "sprint.com",
    //     "type": "isp"
    //   },  
    //   "carrier": {
    //     "name": "Sprint",
    //     "mcc": "310",
    //     "mnc": "120"
    //   }
    // }
    // 

    ip = CkJsonObjectW_stringOf(json,L"ip");
    hostname = CkJsonObjectW_stringOf(json,L"hostname");
    city = CkJsonObjectW_stringOf(json,L"city");
    region = CkJsonObjectW_stringOf(json,L"region");
    country = CkJsonObjectW_stringOf(json,L"country");
    loc = CkJsonObjectW_stringOf(json,L"loc");
    postal = CkJsonObjectW_stringOf(json,L"postal");
    asnAsn = CkJsonObjectW_stringOf(json,L"asn.asn");
    asnName = CkJsonObjectW_stringOf(json,L"asn.name");
    asnDomain = CkJsonObjectW_stringOf(json,L"asn.domain");
    asnRoute = CkJsonObjectW_stringOf(json,L"asn.route");
    asnType = CkJsonObjectW_stringOf(json,L"asn.type");
    companyName = CkJsonObjectW_stringOf(json,L"company.name");
    companyDomain = CkJsonObjectW_stringOf(json,L"company.domain");
    companyType = CkJsonObjectW_stringOf(json,L"company.type");
    carrierName = CkJsonObjectW_stringOf(json,L"carrier.name");
    carrierMcc = CkJsonObjectW_stringOf(json,L"carrier.mcc");
    carrierMnc = CkJsonObjectW_stringOf(json,L"carrier.mnc");


    CkHttpW_Dispose(http);
    CkJsonObjectW_Dispose(json);

    }