Rust
Rust
ipstack.com IPv4 Geolocation Lookup
See more Geolocation Examples
Demonstrates how to lookup Geolocation data for an IPv4 address using the ipstack.com REST API.Chilkat Rust Downloads
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
let http = chilkat::Http::new();
// Lookup an IPv4 address: 149.250.207.170 (this was a randomly chosen address)
let Ok(json_str) = http.quick_get_str("http://api.ipstack.com/149.250.207.170?access_key=YOUR_ACCESS_KEY") 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
// {
// "ip": "149.250.207.170",
// "type": "ipv4",
// "continent_code": "EU",
// "continent_name": "Europe",
// "country_code": "DE",
// "country_name": "Germany",
// "region_code": null,
// "region_name": null,
// "city": null,
// "zip": null,
// "latitude": 51.2993,
// "longitude": 9.491,
// "location": {
// "geoname_id": null,
// "capital": "Berlin",
// "languages": [
// {
// "code": "de",
// "name": "German",
// "native": "Deutsch"
// }
// ],
// "country_flag": "http:\/\/assets.ipstack.com\/flags\/de.svg",
// "country_flag_emoji": "\ud83c\udde9\ud83c\uddea",
// "country_flag_emoji_unicode": "U+1F1E9 U+1F1EA",
// "calling_code": "49",
// "is_eu": true
// }
// }
let mut i: i32 = 0;
let ip = json.string_of("ip").unwrap_or_default();
let v_type = json.string_of("type").unwrap_or_default();
let continent_code = json.string_of("continent_code").unwrap_or_default();
let continent_name = json.string_of("continent_name").unwrap_or_default();
let country_code = json.string_of("country_code").unwrap_or_default();
let country_name = json.string_of("country_name").unwrap_or_default();
let region_code = json.string_of("region_code").unwrap_or_default();
let region_name = json.string_of("region_name").unwrap_or_default();
let city = json.string_of("city").unwrap_or_default();
let zip = json.string_of("zip").unwrap_or_default();
let latitude = json.string_of("latitude").unwrap_or_default();
let longitude = json.string_of("longitude").unwrap_or_default();
let location_geoname_id = json.string_of("location.geoname_id").unwrap_or_default();
let location_capital = json.string_of("location.capital").unwrap_or_default();
let location_country_flag = json.string_of("location.country_flag").unwrap_or_default();
let location_country_flag_emoji = json.string_of("location.country_flag_emoji").unwrap_or_default();
let location_country_flag_emoji_unicode = json.string_of("location.country_flag_emoji_unicode").unwrap_or_default();
let location_calling_code = json.string_of("location.calling_code").unwrap_or_default();
let location_is_eu = json.bool_of("location.is_eu");
i = 0;
let count_i = json.size_of_array("location.languages");
while i < count_i {
json.set_i(i);
let _ = json.string_of("location.languages[i].code").unwrap_or_default();
let _ = json.string_of("location.languages[i].name").unwrap_or_default();
let _ = json.string_of("location.languages[i].native").unwrap_or_default();
i = i + 1;
}