Sample code for 30+ languages & platforms
Rust

MaxMind IPv4 Geolocation Lookup

See more Geolocation Examples

Demonstrates how to lookup Geolocation data for an IPv4 address using the MaxMind GeoIP2 Precision Web Service.

Chilkat Rust Downloads

Rust

// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

let http = chilkat::Http::new();

http.set_login("MAXMIND_ACCOUNT_ID");
http.set_password("MAXMIND_LICENSE_KEY");
http.set_accept("application/json");

// Lookup an IPv4 address: 149.250.207.170  (this was a randomly chosen address)
let Ok(json_str) = http.quick_get_str("https://geoip.maxmind.com/geoip/v2.1/country/149.250.207.170") else {
    println!("{}", http.last_error_text());
    return;
};

let json = chilkat::JsonObject::new();
json.set_emit_compact(false);
let _ = json.load(&json_str).is_ok();

println!("{}", json.emit().unwrap_or_default());

// Sample output:
// Use this online tool to generate parsing code from sample JSON: 
// Generate Parsing Code from JSON

// {
//   "continent": {
//     "code": "EU",
//     "geoname_id": 6255148,
//     "names": {
//       "ja": "?????",
//       "pt-BR": "Europa",
//       "ru": "??????",
//       "zh-CN": "??",
//       "de": "Europa",
//       "en": "Europe",
//       "es": "Europa",
//       "fr": "Europe"
//     }
//   },
//   "country": {
//     "is_in_european_union": true,
//     "iso_code": "DE",
//     "geoname_id": 2921044,
//     "names": {
//       "fr": "Allemagne",
//       "ja": "????????",
//       "pt-BR": "Alemanha",
//       "ru": "????????",
//       "zh-CN": "??",
//       "de": "Deutschland",
//       "en": "Germany",
//       "es": "Alemania"
//     }
//   },
//   "maxmind": {
//     "queries_remaining": 49999
//   },
//   "registered_country": {
//     "is_in_european_union": true,
//     "iso_code": "DE",
//     "geoname_id": 2921044,
//     "names": {
//       "es": "Alemania",
//       "fr": "Allemagne",
//       "ja": "????????",
//       "pt-BR": "Alemanha",
//       "ru": "????????",
//       "zh-CN": "??",
//       "de": "Deutschland",
//       "en": "Germany"
//     }
//   },
//   "traits": {
//     "ip_address": "149.250.207.170"
//   }
// }
// 
// 

let continent_code = json.string_of("continent.code").unwrap_or_default();
let continent_geoname_id = json.int_of("continent.geoname_id");
let continent_names_ja = json.string_of("continent.names.ja").unwrap_or_default();
let continent_names_pt_br = json.string_of("continent.names.pt-BR").unwrap_or_default();
let continent_names_ru = json.string_of("continent.names.ru").unwrap_or_default();
let continent_names_zh_cn = json.string_of("continent.names.zh-CN").unwrap_or_default();
let continent_names_de = json.string_of("continent.names.de").unwrap_or_default();
let continent_names_en = json.string_of("continent.names.en").unwrap_or_default();
let continent_names_es = json.string_of("continent.names.es").unwrap_or_default();
let continent_names_fr = json.string_of("continent.names.fr").unwrap_or_default();
let country_is_in_european_union = json.bool_of("country.is_in_european_union");
let country_iso_code = json.string_of("country.iso_code").unwrap_or_default();
let country_geoname_id = json.int_of("country.geoname_id");
let country_names_fr = json.string_of("country.names.fr").unwrap_or_default();
let country_names_ja = json.string_of("country.names.ja").unwrap_or_default();
let country_names_pt_br = json.string_of("country.names.pt-BR").unwrap_or_default();
let country_names_ru = json.string_of("country.names.ru").unwrap_or_default();
let country_names_zh_cn = json.string_of("country.names.zh-CN").unwrap_or_default();
let country_names_de = json.string_of("country.names.de").unwrap_or_default();
let country_names_en = json.string_of("country.names.en").unwrap_or_default();
let country_names_es = json.string_of("country.names.es").unwrap_or_default();
let maxmind_queries_remaining = json.int_of("maxmind.queries_remaining");
let registered_country_is_in_european_union = json.bool_of("registered_country.is_in_european_union");
let registered_country_iso_code = json.string_of("registered_country.iso_code").unwrap_or_default();
let registered_country_geoname_id = json.int_of("registered_country.geoname_id");
let registered_country_names_es = json.string_of("registered_country.names.es").unwrap_or_default();
let registered_country_names_fr = json.string_of("registered_country.names.fr").unwrap_or_default();
let registered_country_names_ja = json.string_of("registered_country.names.ja").unwrap_or_default();
let registered_country_names_pt_br = json.string_of("registered_country.names.pt-BR").unwrap_or_default();
let registered_country_names_ru = json.string_of("registered_country.names.ru").unwrap_or_default();
let registered_country_names_zh_cn = json.string_of("registered_country.names.zh-CN").unwrap_or_default();
let registered_country_names_de = json.string_of("registered_country.names.de").unwrap_or_default();
let registered_country_names_en = json.string_of("registered_country.names.en").unwrap_or_default();
let traits_ip_address = json.string_of("traits.ip_address").unwrap_or_default();