Unicode C Requires Chilkat v11.0.0+
Unicode C
Look up an IP location with IPinfo Core
See more Geolocation Examples
Use an IPinfo token with Core, Plus, or Max access. The current api.ipinfo.io/lookup/ endpoint returns location fields inside geo and network fields inside as. Authenticate with a Bearer token.
IPinfo continues to support its older ipinfo.io API for existing integrations. This example uses the current Core response structure. The free Lite API uses /lite/ and a different, country-level response. Choose the endpoint and parser for your subscription.
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_CkHttpResponseW.h>
#include <C_CkJsonObjectW.h>
void ChilkatSample(void)
{
BOOL success;
const wchar_t *ipAddress;
HCkHttpW http;
HCkStringBuilderW url;
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");
CkHttpW_putAuthToken(http,L"IPINFO_TOKEN");
url = CkStringBuilderW_Create();
CkStringBuilderW_Append(url,L"https://api.ipinfo.io/lookup/");
CkStringBuilderW_Append(url,CkHttpW_urlEncode(http,ipAddress));
response = CkHttpResponseW_Create();
success = CkHttpW_HttpNoBody(http,L"GET",CkStringBuilderW_getAsString(url),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);
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"error.title") == 1) {
wprintf(L"%s\n",CkJsonObjectW_stringOf(json,L"error.title"));
}
if (CkJsonObjectW_JsonTypeOf(json,L"error.message") == 1) {
wprintf(L"%s\n",CkJsonObjectW_stringOf(json,L"error.message"));
}
}
CkHttpW_Dispose(http);
CkStringBuilderW_Dispose(url);
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);
CkHttpResponseW_Dispose(response);
CkJsonObjectW_Dispose(json);
return;
}
if ((CkJsonObjectW_JsonTypeOf(json,L"error") == 3)) {
wprintf(L"The service could not complete the lookup.\n");
if (CkJsonObjectW_JsonTypeOf(json,L"error.title") == 1) {
wprintf(L"%s\n",CkJsonObjectW_stringOf(json,L"error.title"));
}
if (CkJsonObjectW_JsonTypeOf(json,L"error.message") == 1) {
wprintf(L"%s\n",CkJsonObjectW_stringOf(json,L"error.message"));
}
CkHttpW_Dispose(http);
CkStringBuilderW_Dispose(url);
CkHttpResponseW_Dispose(response);
CkJsonObjectW_Dispose(json);
return;
}
if ((CkJsonObjectW_JsonTypeOf(json,L"bogon") == 5) && (CkJsonObjectW_BoolOf(json,L"bogon") == TRUE)) {
wprintf(L"This is a reserved or otherwise non-public IP address.\n");
CkHttpW_Dispose(http);
CkStringBuilderW_Dispose(url);
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);
CkHttpResponseW_Dispose(response);
CkJsonObjectW_Dispose(json);
return;
}
wprintf(L"IP address: %s\n",CkJsonObjectW_stringOf(json,L"ip"));
if ((CkJsonObjectW_JsonTypeOf(json,L"geo.country") == 1) && (CkJsonObjectW_StringOfEquals(json,L"geo.country",L"",TRUE) != TRUE)) {
wprintf(L"Country: %s\n",CkJsonObjectW_stringOf(json,L"geo.country"));
}
else {
wprintf(L"Country: unavailable\n");
}
if ((CkJsonObjectW_JsonTypeOf(json,L"geo.country_code") == 1) && (CkJsonObjectW_StringOfEquals(json,L"geo.country_code",L"",TRUE) != TRUE)) {
wprintf(L"Country code: %s\n",CkJsonObjectW_stringOf(json,L"geo.country_code"));
}
else {
wprintf(L"Country code: unavailable\n");
}
if ((CkJsonObjectW_JsonTypeOf(json,L"geo.region") == 1) && (CkJsonObjectW_StringOfEquals(json,L"geo.region",L"",TRUE) != TRUE)) {
wprintf(L"Region: %s\n",CkJsonObjectW_stringOf(json,L"geo.region"));
}
else {
wprintf(L"Region: unavailable\n");
}
if ((CkJsonObjectW_JsonTypeOf(json,L"geo.city") == 1) && (CkJsonObjectW_StringOfEquals(json,L"geo.city",L"",TRUE) != TRUE)) {
wprintf(L"City: %s\n",CkJsonObjectW_stringOf(json,L"geo.city"));
}
else {
wprintf(L"City: unavailable\n");
}
if ((CkJsonObjectW_JsonTypeOf(json,L"geo.timezone") == 1) && (CkJsonObjectW_StringOfEquals(json,L"geo.timezone",L"",TRUE) != TRUE)) {
wprintf(L"Time zone: %s\n",CkJsonObjectW_stringOf(json,L"geo.timezone"));
}
else {
wprintf(L"Time zone: unavailable\n");
}
// Missing coordinates stay unavailable. A numeric zero is a valid coordinate.
if ((CkJsonObjectW_JsonTypeOf(json,L"geo.latitude") == 2) && (CkJsonObjectW_JsonTypeOf(json,L"geo.longitude") == 2)) {
wprintf(L"Coordinates: %s, %s\n",CkJsonObjectW_stringOf(json,L"geo.latitude"),CkJsonObjectW_stringOf(json,L"geo.longitude"));
}
else {
wprintf(L"Coordinates: unavailable\n");
}
if ((CkJsonObjectW_JsonTypeOf(json,L"as.asn") == 1) && (CkJsonObjectW_StringOfEquals(json,L"as.asn",L"",TRUE) != TRUE)) {
wprintf(L"ASN: %s\n",CkJsonObjectW_stringOf(json,L"as.asn"));
}
else {
wprintf(L"ASN: unavailable\n");
}
if ((CkJsonObjectW_JsonTypeOf(json,L"as.name") == 1) && (CkJsonObjectW_StringOfEquals(json,L"as.name",L"",TRUE) != TRUE)) {
wprintf(L"Network organization: %s\n",CkJsonObjectW_stringOf(json,L"as.name"));
}
else {
wprintf(L"Network organization: unavailable\n");
}
CkHttpW_Dispose(http);
CkStringBuilderW_Dispose(url);
CkHttpResponseW_Dispose(response);
CkJsonObjectW_Dispose(json);
}