Look up an IP country with MaxMind GeoIP
See more Geolocation Examples
Call the GeoIP Country web service using HTTP Basic authentication. Set the username to your numeric MaxMind account ID and the password to your license key. The account needs access to the web service, such as a trial or service credit. Both IPv4 and IPv6 are supported.
country describes the geolocated country. registered_country describes where the network is registered. The Country service returns country-level data. City-level lookups use the separately entitled /geoip/v2.1/city/ service. A GeoLite web-service account uses geolite.info as its host.
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 Ruby Downloads
require 'chilkat'
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 = Chilkat::CkHttp.new()
http.put_RequireSslCertVerify(true)
http.put_ConnectTimeout(10)
http.put_ReadTimeout(30)
http.put_Accept("application/json")
http.put_BasicAuth(true)
http.put_Login("MAXMIND_ACCOUNT_ID")
http.put_Password("MAXMIND_LICENSE_KEY")
url = Chilkat::CkStringBuilder.new()
url.Append("https://geoip.maxmind.com/geoip/v2.1/country/")
url.Append(http.urlEncode(ipAddress))
response = Chilkat::CkHttpResponse.new()
success = http.HttpNoBody("GET",url.getAsString(),response)
if (response.get_StatusCode() == 0)
# Request diagnostics can contain an API key. Report transport failure without logging credentials.
print "No HTTP response. Check connectivity, DNS, and TLS configuration." + "\n";
exit
end
json = Chilkat::CkJsonObject.new()
parsed = json.Load(response.bodyStr())
if ((success != true) or (response.get_StatusCode() < 200) or (response.get_StatusCode() >= 300))
print "Lookup failed. HTTP " + response.get_StatusCode().to_s() + "\n";
if (parsed == true)
if (json.JsonTypeOf("code") == 1)
print json.stringOf("code") + "\n";
end
if (json.JsonTypeOf("error") == 1)
print json.stringOf("error") + "\n";
end
end
exit
end
if (parsed != true)
print "Expected a JSON object in the lookup response." + "\n";
exit
end
if ((json.JsonTypeOf("traits.ip_address") != 1) or (json.StringOfEquals("traits.ip_address","",true) == true))
print "The response did not contain the expected IP address." + "\n";
exit
end
print "IP address: " + json.stringOf("traits.ip_address") + "\n";
if ((json.JsonTypeOf("country.names.en") == 1) and (json.StringOfEquals("country.names.en","",true) != true))
print "Country: " + json.stringOf("country.names.en") + "\n";
else
print "Country: unavailable" + "\n";
end
if ((json.JsonTypeOf("country.iso_code") == 1) and (json.StringOfEquals("country.iso_code","",true) != true))
print "Country code: " + json.stringOf("country.iso_code") + "\n";
else
print "Country code: unavailable" + "\n";
end
if ((json.JsonTypeOf("continent.names.en") == 1) and (json.StringOfEquals("continent.names.en","",true) != true))
print "Continent: " + json.stringOf("continent.names.en") + "\n";
else
print "Continent: unavailable" + "\n";
end
if ((json.JsonTypeOf("registered_country.iso_code") == 1) and (json.StringOfEquals("registered_country.iso_code","",true) != true))
print "Registered country code: " + json.stringOf("registered_country.iso_code") + "\n";
else
print "Registered country code: unavailable" + "\n";
end