Zig
Zig
geo.ipify.org IPv4 Geolocation Lookup
See more Geolocation Examples
Demonstrates how to lookup Geolocation data for an IPv4 address using the geo.ipify.org 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: 8.8.8.8
const json_str = http.quickGetStr(alloc, "https://geo.ipify.org/api/v1?apiKey=API_KEY&ipAddress=8.8.8.8") 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": "8.8.8.8",
// "location": {
// "country": "IT",
// "region": "Lombardy",
// "city": "Milan",
// "lat": 45.4707,
// "lng": 9.1889,
// "postalCode": "20147",
// "timezone": "+02:00"
// }
// }
_ = try json.stringOf(alloc, "ip");
_ = try json.stringOf(alloc, "location.country");
_ = try json.stringOf(alloc, "location.region");
_ = try json.stringOf(alloc, "location.city");
_ = try json.stringOf(alloc, "location.lat");
_ = try json.stringOf(alloc, "location.lng");
_ = try json.stringOf(alloc, "location.postalCode");
_ = try json.stringOf(alloc, "location.timezone");
}