Zig
Zig
Shippo Validate Address
See more Shippo Examples
Demonstrates how to validate your addresses through the Shippo API before you create labels.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 assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
const http = try chilkat.Http.init();
defer http.deinit();
// Implements the following CURL command:
// curl https://api.goshippo.com/addresses/ \
// -H "Authorization: ShippoToken <API_TOKEN>" \
// -d name="Shawn Ippotle" \
// -d company="Shippo" \
// -d street1="215 Clayton St." \
// -d city="San Francisco" \
// -d state="CA" \
// -d zip=94117 \
// -d country="US" \
// -d email="shippotle@goshippo.com"\
// -d validate=true
const req = try chilkat.HttpRequest.init();
defer req.deinit();
req.setHttpVerb("POST");
req.setPath("/addresses/");
req.setContentType("application/x-www-form-urlencoded");
req.addParam("name", "Shawn Ippotle");
req.addParam("company", "Shippo");
req.addParam("street1", "215 Clayton St.");
req.addParam("city", "San Francisco");
req.addParam("state", "CA");
req.addParam("zip", "94117");
req.addParam("country", "US");
req.addParam("email", "shippotle@goshippo.com");
req.addParam("validate", "true");
req.addHeader("Authorization", "ShippoToken <API_TOKEN>");
const resp = try chilkat.HttpResponse.init();
defer resp.deinit();
http.httpReq("https://api.goshippo.com/addresses/", req, resp) catch {
std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
return;
};
const sb_response_body = try chilkat.StringBuilder.init();
defer sb_response_body.deinit();
resp.getBodySb(sb_response_body) catch {};
const j_resp = try chilkat.JsonObject.init();
defer j_resp.deinit();
j_resp.loadSb(sb_response_body) catch {};
j_resp.setEmitCompact(false);
std.debug.print("Response Body:\n", .{});
std.debug.print("{s}\n", .{try j_resp.emit(alloc)});
const resp_status_code = resp.getStatusCode();
std.debug.print("Response Status Code = {d}\n", .{resp_status_code});
if (resp_status_code >= 400) {
std.debug.print("Response Header:\n", .{});
std.debug.print("{s}\n", .{try resp.getHeader(alloc)});
std.debug.print("Failed.\n", .{});
return;
}
// Sample JSON response:
// (Sample code for parsing the JSON response is shown below)
// {
// "object_created": "2015-03-30T23:47:11.574Z",
// "object_updated": "2015-03-30T23:47:11.596Z",
// "object_id": "67183b2e81e9421f894bfbcdc4236b16",
// "is_complete": false,
// "validation_results": {
// "is_valid": false,
// "messages": [
// {
// "source": "USPS",
// "code": "Address Not Found",
// "type": "address_error",
// "text": "The address as submitted could not be found. Please check for excessive abbreviations in the street address line or in the City name."
// }
// ]
// },
// "object_owner": "shippotle@goshippo.com",
// "name": "Shawn Ippotle",
// "company": "Shippo",
// "street_no": "",
// "street1": "215 HIPPO ST.",
// "street2": "",
// "city": "SAN FRANCISCO",
// "state": "CA",
// "zip": "94107",
// "country": "US",
// "phone": ""
// }
// Sample code for parsing the JSON response...
// Use the following online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON
var source: [:0]const u8 = "";
var code: [:0]const u8 = "";
var v_type: [:0]const u8 = "";
var text: [:0]const u8 = "";
_ = try j_resp.stringOf(alloc, "object_created");
_ = try j_resp.stringOf(alloc, "object_updated");
_ = try j_resp.stringOf(alloc, "object_id");
_ = j_resp.boolOf("is_complete");
_ = j_resp.boolOf("validation_results.is_valid");
_ = try j_resp.stringOf(alloc, "object_owner");
_ = try j_resp.stringOf(alloc, "name");
_ = try j_resp.stringOf(alloc, "company");
_ = try j_resp.stringOf(alloc, "street_no");
_ = try j_resp.stringOf(alloc, "street1");
_ = try j_resp.stringOf(alloc, "street2");
_ = try j_resp.stringOf(alloc, "city");
_ = try j_resp.stringOf(alloc, "state");
_ = try j_resp.stringOf(alloc, "zip");
_ = try j_resp.stringOf(alloc, "country");
_ = try j_resp.stringOf(alloc, "phone");
var i: i32 = 0;
const count_i = j_resp.sizeOfArray("validation_results.messages");
while (i < count_i) {
j_resp.setI(i);
source = try j_resp.stringOf(alloc, "validation_results.messages[i].source");
code = try j_resp.stringOf(alloc, "validation_results.messages[i].code");
v_type = try j_resp.stringOf(alloc, "validation_results.messages[i].type");
text = try j_resp.stringOf(alloc, "validation_results.messages[i].text");
i = i + 1;
}
}