Zig
Zig
ipapi.co IPv4 Geolocation Lookup
See more Geolocation Examples
Demonstrates how to lookup Geolocation data for an IPv4 address using the ipapi.co REST API.Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
const http = try chilkat.Http.init();
defer http.deinit();
// Lookup an IPv4 address: 149.250.207.170 (this was a randomly chosen address)
const json_str = http.quickGetStr(alloc, "https://ipapi.co/149.250.207.170/json") catch {
std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
return;
};
const json = try chilkat.JsonObject.init();
defer json.deinit();
json.setEmitCompact(false);
try json.load(json_str);
std.debug.print("{s}\n", .{try json.emit(alloc)});
// Sample output:
// Use this online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON
// {
// "ip": "149.250.207.170",
// "city": "B�blingen",
// "region": "Baden-W�rttemberg",
// "region_code": null,
// "country": "DE",
// "country_name": "Germany",
// "continent_code": "EU",
// "in_eu": true,
// "postal": null,
// "latitude": null,
// "longitude": null,
// "timezone": null,
// "utc_offset": null,
// "country_calling_code": "+49",
// "currency": "EUR",
// "languages": "de",
// "asn": "AS15854",
// "org": "EntServ Deutschland GmbH"
// }
_ = try json.stringOf(alloc, "ip");
_ = try json.stringOf(alloc, "city");
_ = try json.stringOf(alloc, "region");
_ = try json.stringOf(alloc, "region_code");
_ = try json.stringOf(alloc, "country");
_ = try json.stringOf(alloc, "country_name");
_ = try json.stringOf(alloc, "continent_code");
_ = json.boolOf("in_eu");
_ = try json.stringOf(alloc, "postal");
_ = try json.stringOf(alloc, "latitude");
_ = try json.stringOf(alloc, "longitude");
_ = try json.stringOf(alloc, "timezone");
_ = try json.stringOf(alloc, "utc_offset");
_ = try json.stringOf(alloc, "country_calling_code");
_ = try json.stringOf(alloc, "currency");
_ = try json.stringOf(alloc, "languages");
_ = try json.stringOf(alloc, "asn");
_ = try json.stringOf(alloc, "org");
}