C Requires Chilkat v11.0.0+
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 C Downloads
#include <C_CkHttp.h>
#include <C_CkStringBuilder.h>
#include <C_CkJsonObject.h>
#include <C_CkHttpResponse.h>
void ChilkatSample(void)
{
BOOL success;
const char *ipAddress;
HCkHttp http;
HCkStringBuilder url;
HCkJsonObject parameters;
HCkHttpResponse response;
const char *remaining;
HCkJsonObject 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 = "8.8.8.8";
http = CkHttp_Create();
CkHttp_putRequireSslCertVerify(http,TRUE);
CkHttp_putConnectTimeout(http,10);
CkHttp_putReadTimeout(http,30);
CkHttp_putAccept(http,"application/json");
// The free endpoint is HTTP-only. This single lookup sends no API credentials.
url = CkStringBuilder_Create();
CkStringBuilder_Append(url,"http://ip-api.com/json/");
CkStringBuilder_Append(url,CkHttp_urlEncode(http,ipAddress));
parameters = CkJsonObject_Create();
CkJsonObject_UpdateString(parameters,"fields","status,message,query,country,countryCode,regionName,city,lat,lon,timezone");
response = CkHttpResponse_Create();
success = CkHttp_HttpParams(http,"GET",CkStringBuilder_getAsString(url),parameters,response);
if (CkHttpResponse_getStatusCode(response) == 0) {
// Request diagnostics can contain an API key. Report transport failure without logging credentials.
printf("No HTTP response. Check connectivity, DNS, and TLS configuration.\n");
CkHttp_Dispose(http);
CkStringBuilder_Dispose(url);
CkJsonObject_Dispose(parameters);
CkHttpResponse_Dispose(response);
return;
}
// Save these values if the application will make further requests.
remaining = CkHttpResponse_getHeaderField(response,"X-Rl");
if (remaining == "0") {
printf("Rate limit reached. Wait %s seconds before the next request.\n",CkHttpResponse_getHeaderField(response,"X-Ttl"));
}
json = CkJsonObject_Create();
parsed = CkJsonObject_Load(json,CkHttpResponse_bodyStr(response));
if ((success != TRUE) || (CkHttpResponse_getStatusCode(response) < 200) || (CkHttpResponse_getStatusCode(response) >= 300)) {
printf("Lookup failed. HTTP %d\n",CkHttpResponse_getStatusCode(response));
if (parsed == TRUE) {
if (CkJsonObject_JsonTypeOf(json,"message") == 1) {
printf("%s\n",CkJsonObject_stringOf(json,"message"));
}
}
CkHttp_Dispose(http);
CkStringBuilder_Dispose(url);
CkJsonObject_Dispose(parameters);
CkHttpResponse_Dispose(response);
CkJsonObject_Dispose(json);
return;
}
if (parsed != TRUE) {
printf("Expected a JSON object in the lookup response.\n");
CkHttp_Dispose(http);
CkStringBuilder_Dispose(url);
CkJsonObject_Dispose(parameters);
CkHttpResponse_Dispose(response);
CkJsonObject_Dispose(json);
return;
}
if (CkJsonObject_StringOfEquals(json,"status","success",TRUE) != TRUE) {
printf("The service could not complete the lookup.\n");
if (CkJsonObject_JsonTypeOf(json,"message") == 1) {
printf("%s\n",CkJsonObject_stringOf(json,"message"));
}
CkHttp_Dispose(http);
CkStringBuilder_Dispose(url);
CkJsonObject_Dispose(parameters);
CkHttpResponse_Dispose(response);
CkJsonObject_Dispose(json);
return;
}
if ((CkJsonObject_JsonTypeOf(json,"query") != 1) || (CkJsonObject_StringOfEquals(json,"query","",TRUE) == TRUE)) {
printf("The response did not contain the expected IP address.\n");
CkHttp_Dispose(http);
CkStringBuilder_Dispose(url);
CkJsonObject_Dispose(parameters);
CkHttpResponse_Dispose(response);
CkJsonObject_Dispose(json);
return;
}
printf("IP address: %s\n",CkJsonObject_stringOf(json,"query"));
if ((CkJsonObject_JsonTypeOf(json,"country") == 1) && (CkJsonObject_StringOfEquals(json,"country","",TRUE) != TRUE)) {
printf("Country: %s\n",CkJsonObject_stringOf(json,"country"));
}
else {
printf("Country: unavailable\n");
}
if ((CkJsonObject_JsonTypeOf(json,"countryCode") == 1) && (CkJsonObject_StringOfEquals(json,"countryCode","",TRUE) != TRUE)) {
printf("Country code: %s\n",CkJsonObject_stringOf(json,"countryCode"));
}
else {
printf("Country code: unavailable\n");
}
if ((CkJsonObject_JsonTypeOf(json,"regionName") == 1) && (CkJsonObject_StringOfEquals(json,"regionName","",TRUE) != TRUE)) {
printf("Region: %s\n",CkJsonObject_stringOf(json,"regionName"));
}
else {
printf("Region: unavailable\n");
}
if ((CkJsonObject_JsonTypeOf(json,"city") == 1) && (CkJsonObject_StringOfEquals(json,"city","",TRUE) != TRUE)) {
printf("City: %s\n",CkJsonObject_stringOf(json,"city"));
}
else {
printf("City: unavailable\n");
}
if ((CkJsonObject_JsonTypeOf(json,"timezone") == 1) && (CkJsonObject_StringOfEquals(json,"timezone","",TRUE) != TRUE)) {
printf("Time zone: %s\n",CkJsonObject_stringOf(json,"timezone"));
}
else {
printf("Time zone: unavailable\n");
}
// Missing coordinates stay unavailable. A numeric zero is a valid coordinate.
if ((CkJsonObject_JsonTypeOf(json,"lat") == 2) && (CkJsonObject_JsonTypeOf(json,"lon") == 2)) {
printf("Coordinates: %s, %s\n",CkJsonObject_stringOf(json,"lat"),CkJsonObject_stringOf(json,"lon"));
}
else {
printf("Coordinates: unavailable\n");
}
CkHttp_Dispose(http);
CkStringBuilder_Dispose(url);
CkJsonObject_Dispose(parameters);
CkHttpResponse_Dispose(response);
CkJsonObject_Dispose(json);
}