Rust
Rust
api.db-ip.com IPv4 Geolocation Lookup
See more Geolocation Examples
Demonstrates how to lookup Geolocation data for an IPv4 address using the api.db-ip.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.db-ip.com/v2/free/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
// {
// "ipAddress": "149.250.207.170",
// "continentCode": "EU",
// "continentName": "Europe",
// "countryCode": "DE",
// "countryName": "Germany",
// "stateProv": "Baden-W\u00fcrttemberg",
// "city": "B\u00f6blingen"
// }
let ip_address = json.string_of("ipAddress").unwrap_or_default();
let continent_code = json.string_of("continentCode").unwrap_or_default();
let continent_name = json.string_of("continentName").unwrap_or_default();
let country_code = json.string_of("countryCode").unwrap_or_default();
let country_name = json.string_of("countryName").unwrap_or_default();
let state_prov = json.string_of("stateProv").unwrap_or_default();
let city = json.string_of("city").unwrap_or_default();