Unicode C Requires Chilkat v11.0.0+
Unicode C
Look up an IP address with the ip-api free endpoint
See more Geolocation Examples
This example uses the free, keyless ip-api endpoint for non-commercial use. The free service supports HTTP and is limited to 45 requests per minute per source IP. Its Pro service provides HTTPS and commercial use. ip-api Pro.
Always include status and message when selecting fields. Invalid or private addresses can produce status=fail inside a JSON response. Retain the X-Rl and X-Ttl response values between calls, and pause further requests when X-Rl reaches zero.
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;
HCkStringBuilderW url;
HCkJsonObjectW parameters;
HCkHttpResponseW response;
const wchar_t *remaining;
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");
// The free endpoint is HTTP-only. This single lookup sends no API credentials.
url = CkStringBuilderW_Create();
CkStringBuilderW_Append(url,L"http://ip-api.com/json/");
CkStringBuilderW_Append(url,CkHttpW_urlEncode(http,ipAddress));
parameters = CkJsonObjectW_Create();
CkJsonObjectW_UpdateString(parameters,L"fields",L"status,message,query,country,countryCode,regionName,city,lat,lon,timezone");
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;
}
// Save these values if the application will make further requests.
remaining = CkHttpResponseW_getHeaderField(response,L"X-Rl");
if (remaining == L"0") {
wprintf(L"Rate limit reached. Wait %s seconds before the next request.\n",CkHttpResponseW_getHeaderField(response,L"X-Ttl"));
}
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_StringOfEquals(json,L"status",L"success",TRUE) != TRUE) {
wprintf(L"The service could not complete the lookup.\n");
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 ((CkJsonObjectW_JsonTypeOf(json,L"query") != 1) || (CkJsonObjectW_StringOfEquals(json,L"query",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"query"));
if ((CkJsonObjectW_JsonTypeOf(json,L"country") == 1) && (CkJsonObjectW_StringOfEquals(json,L"country",L"",TRUE) != TRUE)) {
wprintf(L"Country: %s\n",CkJsonObjectW_stringOf(json,L"country"));
}
else {
wprintf(L"Country: unavailable\n");
}
if ((CkJsonObjectW_JsonTypeOf(json,L"countryCode") == 1) && (CkJsonObjectW_StringOfEquals(json,L"countryCode",L"",TRUE) != TRUE)) {
wprintf(L"Country code: %s\n",CkJsonObjectW_stringOf(json,L"countryCode"));
}
else {
wprintf(L"Country code: unavailable\n");
}
if ((CkJsonObjectW_JsonTypeOf(json,L"regionName") == 1) && (CkJsonObjectW_StringOfEquals(json,L"regionName",L"",TRUE) != TRUE)) {
wprintf(L"Region: %s\n",CkJsonObjectW_stringOf(json,L"regionName"));
}
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"timezone") == 1) && (CkJsonObjectW_StringOfEquals(json,L"timezone",L"",TRUE) != TRUE)) {
wprintf(L"Time zone: %s\n",CkJsonObjectW_stringOf(json,L"timezone"));
}
else {
wprintf(L"Time zone: unavailable\n");
}
// Missing coordinates stay unavailable. A numeric zero is a valid coordinate.
if ((CkJsonObjectW_JsonTypeOf(json,L"lat") == 2) && (CkJsonObjectW_JsonTypeOf(json,L"lon") == 2)) {
wprintf(L"Coordinates: %s, %s\n",CkJsonObjectW_stringOf(json,L"lat"),CkJsonObjectW_stringOf(json,L"lon"));
}
else {
wprintf(L"Coordinates: unavailable\n");
}
CkHttpW_Dispose(http);
CkStringBuilderW_Dispose(url);
CkJsonObjectW_Dispose(parameters);
CkHttpResponseW_Dispose(response);
CkJsonObjectW_Dispose(json);
}