Unicode C Requires Chilkat v11.0.0+
Unicode C
Look up an IP address with ipdata
See more Geolocation Examples
Use an ipdata API key to request a public IPv4 or IPv6 address. The key is encoded as the api-key query parameter. The example displays location fields and the optional ASN object using its current nested structure. Data access and quotas depend on the account plan.
IP geolocation estimates a network location. VPNs, mobile gateways, and anycast routing can affect the result. City and coordinate fields can be missing or approximate. The sample address 8.8.8.8 is a public DNS address used to demonstrate the request.
Chilkat Unicode C Downloads
#include <C_CkHttpW.h>
#include <C_CkStringBuilderW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkHttpResponseW.h>
void ChilkatSample(void)
{
BOOL success;
const wchar_t *ipAddress;
HCkHttpW http;
const wchar_t *apiKey;
HCkStringBuilderW url;
HCkJsonObjectW parameters;
HCkHttpResponseW response;
HCkJsonObjectW json;
BOOL parsed;
success = FALSE;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
// Use a public IP address. Provider results can change as their databases are updated.
ipAddress = L"8.8.8.8";
http = CkHttpW_Create();
CkHttpW_putRequireSslCertVerify(http,TRUE);
CkHttpW_putConnectTimeout(http,10);
CkHttpW_putReadTimeout(http,30);
CkHttpW_putAccept(http,L"application/json");
apiKey = L"YOUR_API_KEY";
url = CkStringBuilderW_Create();
CkStringBuilderW_Append(url,L"https://api.ipdata.co/");
CkStringBuilderW_Append(url,CkHttpW_urlEncode(http,ipAddress));
parameters = CkJsonObjectW_Create();
CkJsonObjectW_UpdateString(parameters,L"api-key",apiKey);
response = CkHttpResponseW_Create();
success = CkHttpW_HttpParams(http,L"GET",CkStringBuilderW_getAsString(url),parameters,response);
if (CkHttpResponseW_getStatusCode(response) == 0) {
// Request diagnostics can contain an API key. Report transport failure without logging credentials.
wprintf(L"No HTTP response. Check connectivity, DNS, and TLS configuration.\n");
CkHttpW_Dispose(http);
CkStringBuilderW_Dispose(url);
CkJsonObjectW_Dispose(parameters);
CkHttpResponseW_Dispose(response);
return;
}
json = CkJsonObjectW_Create();
parsed = CkJsonObjectW_Load(json,CkHttpResponseW_bodyStr(response));
if ((success != TRUE) || (CkHttpResponseW_getStatusCode(response) < 200) || (CkHttpResponseW_getStatusCode(response) >= 300)) {
wprintf(L"Lookup failed. HTTP %d\n",CkHttpResponseW_getStatusCode(response));
if (parsed == TRUE) {
if (CkJsonObjectW_JsonTypeOf(json,L"message") == 1) {
wprintf(L"%s\n",CkJsonObjectW_stringOf(json,L"message"));
}
}
CkHttpW_Dispose(http);
CkStringBuilderW_Dispose(url);
CkJsonObjectW_Dispose(parameters);
CkHttpResponseW_Dispose(response);
CkJsonObjectW_Dispose(json);
return;
}
if (parsed != TRUE) {
wprintf(L"Expected a JSON object in the lookup response.\n");
CkHttpW_Dispose(http);
CkStringBuilderW_Dispose(url);
CkJsonObjectW_Dispose(parameters);
CkHttpResponseW_Dispose(response);
CkJsonObjectW_Dispose(json);
return;
}
if ((CkJsonObjectW_JsonTypeOf(json,L"ip") != 1) || (CkJsonObjectW_StringOfEquals(json,L"ip",L"",TRUE) == TRUE)) {
wprintf(L"The response did not contain the expected IP address.\n");
CkHttpW_Dispose(http);
CkStringBuilderW_Dispose(url);
CkJsonObjectW_Dispose(parameters);
CkHttpResponseW_Dispose(response);
CkJsonObjectW_Dispose(json);
return;
}
wprintf(L"IP address: %s\n",CkJsonObjectW_stringOf(json,L"ip"));
if ((CkJsonObjectW_JsonTypeOf(json,L"country_name") == 1) && (CkJsonObjectW_StringOfEquals(json,L"country_name",L"",TRUE) != TRUE)) {
wprintf(L"Country: %s\n",CkJsonObjectW_stringOf(json,L"country_name"));
}
else {
wprintf(L"Country: unavailable\n");
}
if ((CkJsonObjectW_JsonTypeOf(json,L"country_code") == 1) && (CkJsonObjectW_StringOfEquals(json,L"country_code",L"",TRUE) != TRUE)) {
wprintf(L"Country code: %s\n",CkJsonObjectW_stringOf(json,L"country_code"));
}
else {
wprintf(L"Country code: unavailable\n");
}
if ((CkJsonObjectW_JsonTypeOf(json,L"region") == 1) && (CkJsonObjectW_StringOfEquals(json,L"region",L"",TRUE) != TRUE)) {
wprintf(L"Region: %s\n",CkJsonObjectW_stringOf(json,L"region"));
}
else {
wprintf(L"Region: unavailable\n");
}
if ((CkJsonObjectW_JsonTypeOf(json,L"city") == 1) && (CkJsonObjectW_StringOfEquals(json,L"city",L"",TRUE) != TRUE)) {
wprintf(L"City: %s\n",CkJsonObjectW_stringOf(json,L"city"));
}
else {
wprintf(L"City: unavailable\n");
}
if ((CkJsonObjectW_JsonTypeOf(json,L"time_zone.name") == 1) && (CkJsonObjectW_StringOfEquals(json,L"time_zone.name",L"",TRUE) != TRUE)) {
wprintf(L"Time zone: %s\n",CkJsonObjectW_stringOf(json,L"time_zone.name"));
}
else {
wprintf(L"Time zone: unavailable\n");
}
// Missing coordinates stay unavailable. A numeric zero is a valid coordinate.
if ((CkJsonObjectW_JsonTypeOf(json,L"latitude") == 2) && (CkJsonObjectW_JsonTypeOf(json,L"longitude") == 2)) {
wprintf(L"Coordinates: %s, %s\n",CkJsonObjectW_stringOf(json,L"latitude"),CkJsonObjectW_stringOf(json,L"longitude"));
}
else {
wprintf(L"Coordinates: unavailable\n");
}
if ((CkJsonObjectW_JsonTypeOf(json,L"asn.asn") == 1) && (CkJsonObjectW_StringOfEquals(json,L"asn.asn",L"",TRUE) != TRUE)) {
wprintf(L"ASN: %s\n",CkJsonObjectW_stringOf(json,L"asn.asn"));
}
else {
wprintf(L"ASN: unavailable\n");
}
if ((CkJsonObjectW_JsonTypeOf(json,L"asn.name") == 1) && (CkJsonObjectW_StringOfEquals(json,L"asn.name",L"",TRUE) != TRUE)) {
wprintf(L"Network organization: %s\n",CkJsonObjectW_stringOf(json,L"asn.name"));
}
else {
wprintf(L"Network organization: unavailable\n");
}
CkHttpW_Dispose(http);
CkStringBuilderW_Dispose(url);
CkJsonObjectW_Dispose(parameters);
CkHttpResponseW_Dispose(response);
CkJsonObjectW_Dispose(json);
}