Sample code for 30+ languages & platforms
Zig

UPS Address Validation (City, State, Zip)

See more HTTP Misc Examples

Demonstrates making a call to the UPS address validation REST API.

Chilkat Zig Downloads

Zig
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();

    // This is the testing endpoint for address validation:
    const url = "https://wwwcie.ups.com/rest/AV";

    // Send an HTTP request with the following JSON body:

    // {
    //   "AccessRequest": {
    //     "AccessLicenseNumber": "Your Access License Number",
    //     "UserId": "Your Username",
    //     "Password": "Your Password"
    //   },
    //   "AddressValidationRequest": {
    //     "Request": {
    //       "TransactionReference": {
    //         "CustomerContext": "Your Customer Context"
    //       },
    //       "RequestAction": "AV"
    //     },
    //     "Address": {
    //       "City": "ALPHARETTA",
    //       "StateProvinceCode": "GA",
    //       "PostalCode": "30005"
    //     }
    //   }
    // }

    // Build the above JSON.
    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.updateString("AccessRequest.AccessLicenseNumber", "UPS_ACCESS_KEY") catch {};
    json.updateString("AccessRequest.UserId", "UPS_USERNAME") catch {};
    json.updateString("AccessRequest.Password", "UPS_PASSWORD") catch {};
    json.updateString("AddressValidationRequest.Request.TransactionReference.CustomerContext", "Your Customer Context") catch {};
    json.updateString("AddressValidationRequest.Request.RequestAction", "AV") catch {};
    json.updateString("AddressValidationRequest.Address.City", "ALPHARETTA") catch {};
    // We're making an intentional mistake here by passing CA instead of GA.
    json.updateString("AddressValidationRequest.Address.StateProvinceCode", "CA") catch {};
    json.updateString("AddressValidationRequest.Address.PostalCode", "30005") catch {};

    const sb = try chilkat.StringBuilder.init();
    defer sb.deinit();
    const resp = try chilkat.HttpResponse.init();
    defer resp.deinit();
    http.httpJson("POST", url, json, "application/json", resp) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("status = {d}\n", .{resp.getStatusCode()});

    // A 200 response status indicate success.
    if (resp.getStatusCode() != 200) {
        std.debug.print("{s}\n", .{try resp.getBodyStr(alloc)});
        std.debug.print("Failed.\n", .{});
        return;
    }

    json.load(try resp.getBodyStr(alloc)) catch {};
    json.setEmitCompact(false);
    std.debug.print("{s}\n", .{try json.emit(alloc)});

    // A successful exact response looks like this:

    // {
    //   "AddressValidationResponse": {
    //     "Response": {
    //       "TransactionReference": {
    //         "CustomerContext": "Your Customer Context"
    //       },
    //       "ResponseStatusCode": "1",
    //       "ResponseStatusDescription": "Success"
    //     },
    //     "AddressValidationResult": {
    //       "Rank": "1",
    //       "Quality": "1.0",
    //       "Address": {
    //         "City": "ALPHARETTA",
    //         "StateProvinceCode": "GA"
    //       },
    //       "PostalCodeLowEnd": "30005",
    //       "PostalCodeHighEnd": "30005"
    //     }
    //   }
    // }
    //

    // A successful response that was not an exact match provides an array of closest matches, like this:

    // {
    //   "AddressValidationResponse": {
    //     "Response": {
    //       "TransactionReference": {
    //         "CustomerContext": "Your Customer Context"
    //         "Quality": "0.9875",
    //         "Address": {
    //       },
    //       "ResponseStatusCode": "1",
    //       "ResponseStatusDescription": "Success"
    //     },
    //     "AddressValidationResult": [
    //       {
    //         "Rank": "1",
    //           "City": "ALPHARETTA",
    //           "StateProvinceCode": "GA"
    //         },
    //         "PostalCodeLowEnd": "30005",
    //         "PostalCodeHighEnd": "30005"
    //       },
    //       {
    //         "Rank": "2",
    //         "Quality": "0.9750",
    //         "Address": {
    //           "City": "ALPHARETTA",
    //           "StateProvinceCode": "GA"
    //         },
    //         "PostalCodeLowEnd": "30004",
    //         "PostalCodeHighEnd": "30004"
    //       },
    //       {
    //         "Rank": "3",
    //         "Quality": "0.9750",
    //         "Address": {
    //           "City": "ALPHARETTA",
    //           "StateProvinceCode": "GA"
    //         },
    //         "PostalCodeLowEnd": "30009",
    //         "PostalCodeHighEnd": "30009"
    //       }
    //     ]
    //   }
    // }

    // Use the online tool at Generate JSON Parsing Code
    // to generate JSON parsing code.

    var customer_context: [:0]const u8 = "";
    var status_code: [:0]const u8 = "";
    var status_description: [:0]const u8 = "";
    var result_rank: [:0]const u8 = "";
    var result_quality: [:0]const u8 = "";
    var city: [:0]const u8 = "";
    var province_code: [:0]const u8 = "";
    var postal_code_low_end: [:0]const u8 = "";
    var postal_code_high_end: [:0]const u8 = "";
    var rank: [:0]const u8 = "";
    var quality: [:0]const u8 = "";
    var address_city: [:0]const u8 = "";
    var address_state_province_code: [:0]const u8 = "";

    const num_results = json.sizeOfArray("AddressValidationResponse.AddressValidationResult");
    if (num_results < 0) {
        // Here's parse code for the above JSON exact response:
        customer_context = try json.stringOf(alloc, "AddressValidationResponse.Response.TransactionReference.CustomerContext");
        status_code = try json.stringOf(alloc, "AddressValidationResponse.Response.ResponseStatusCode");
        status_description = try json.stringOf(alloc, "AddressValidationResponse.Response.ResponseStatusDescription");
        result_rank = try json.stringOf(alloc, "AddressValidationResponse.AddressValidationResult.Rank");
        result_quality = try json.stringOf(alloc, "AddressValidationResponse.AddressValidationResult.Quality");
        city = try json.stringOf(alloc, "AddressValidationResponse.AddressValidationResult.Address.City");
        province_code = try json.stringOf(alloc, "AddressValidationResponse.AddressValidationResult.Address.StateProvinceCode");
        postal_code_low_end = try json.stringOf(alloc, "AddressValidationResponse.AddressValidationResult.PostalCodeLowEnd");
        postal_code_high_end = try json.stringOf(alloc, "AddressValidationResponse.AddressValidationResult.PostalCodeHighEnd");

        std.debug.print("Exact match!\n", .{});
        std.debug.print("postal code: {s}\n", .{postal_code_low_end});
    } else {
        std.debug.print("Non-Exact match.\n", .{});

        customer_context = try json.stringOf(alloc, "AddressValidationResponse.Response.TransactionReference.CustomerContext");
        status_code = try json.stringOf(alloc, "AddressValidationResponse.Response.ResponseStatusCode");
        status_description = try json.stringOf(alloc, "AddressValidationResponse.Response.ResponseStatusDescription");
        var i: i32 = 0;
        while (i < num_results) {
            json.setI(i);
            rank = try json.stringOf(alloc, "AddressValidationResponse.AddressValidationResult[i].Rank");
            std.debug.print("rank: {s}\n", .{rank});
            quality = try json.stringOf(alloc, "AddressValidationResponse.AddressValidationResult[i].Quality");
            address_city = try json.stringOf(alloc, "AddressValidationResponse.AddressValidationResult[i].Address.City");
            std.debug.print("addressCity: {s}\n", .{address_city});
            address_state_province_code = try json.stringOf(alloc, "AddressValidationResponse.AddressValidationResult[i].Address.StateProvinceCode");
            postal_code_low_end = try json.stringOf(alloc, "AddressValidationResponse.AddressValidationResult[i].PostalCodeLowEnd");
            std.debug.print("postal code: {s}\n", .{postal_code_low_end});
            postal_code_high_end = try json.stringOf(alloc, "AddressValidationResponse.AddressValidationResult[i].PostalCodeHighEnd");
            i = i + 1;
        }
    }
}