Zig
Zig
UPS Tracking API
See more HTTP Misc Examples
Demonstrates making a call to the UPS tracking REST API. Parses the tracking response and extracts the base64 signature image to a gif file.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();
// This is the testing endpoint for the tracking API:
const url = "https://wwwcie.ups.com/rest/Track";
// Send an HTTP request with the following JSON body:
// {
// "UPSSecurity": {
// "UsernameToken": {
// "Username": "Your Username",
// "Password": "Your Password"
// },
// "ServiceAccessToken": {
// "AccessLicenseNumber": "Your Access License Number"
// }
// },
// "TrackRequest": {
// "Request": {
// "RequestOption": "1",
// "TransactionReference": {
// "CustomerContext": "Your Test Case Summary Description"
// }
// },
// "InquiryNumber": "YourTrackingNumber"
// }
// }
//
// Build the above JSON.
const json = try chilkat.JsonObject.init();
defer json.deinit();
json.updateString("UPSSecurity.UsernameToken.Username", "UPS_USERNAME") catch {};
json.updateString("UPSSecurity.UsernameToken.Password", "UPS_PASSWORD") catch {};
json.updateString("UPSSecurity.ServiceAccessToken.AccessLicenseNumber", "UPS_ACCESS_KEY") catch {};
// Request all activity...
json.updateString("TrackRequest.Request.RequestOption", "activity") catch {};
json.updateString("TrackRequest.Request.TransactionReference.CustomerContext", "Your Test Case Summary Description") catch {};
json.updateString("TrackRequest.InquiryNumber", "1Z12345E0205271688") 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)});
// {
// "TrackResponse": {
// "Response": {
// "ResponseStatus": {
// "Code": "1",
// "Description": "Success"
// },
// "TransactionReference": {
// "CustomerContext": "Your Test Case Summary Description"
// }
// },
// "Shipment": {
// "InquiryNumber": {
// "Code": "01",
// "Description": "ShipmentIdentificationNumber",
// "Value": "1Z12345E0205271688"
// },
// "ShipmentType": {
// "Code": "01",
// "Description": "Small Package"
// },
// "ShipperNumber": "12345E",
// "Service": {
// "Code": "002",
// "Description": "2ND DAY AIR"
// },
// "ReferenceNumber": {
// "Code": "01",
// "Value": "LINE4AND115"
// },
// "PickupDate": "19990608",
// "Package": {
// "TrackingNumber": "1Z12345E0205271688",
// "Activity": [
// {
// "ActivityLocation": {
// "Address": {
// "City": "ANYTOWN",
// "StateProvinceCode": "GA",
//
// "PostalCode": "30340",
// "CountryCode": "US"
// },
// "Code": "ML",
// "Description": "BACK DOOR",
// "SignedForByName": "JOHN DOE"
// },
// "Status": {
// "Type": "D",
// "Description": "DELIVERED",
// "Code": "KM"
// },
// "Date": "19990610",
// "Time": "120000",
// "Document": {
// "Type": {
// "Code": "01",
// "Description": "Signature Image"
// },
// "Content": "R0lGODdhoA ... JU9Y8RdHsRKLMVJ4MVDMREAAADs=",
// "Format": {
// "Code": "01",
// "Description": "GIF"
// }
// }
// },
// {
// "Status": {
// "Type": "M",
// "Description": "BILLING INFORMATION RECEIVED. SHIPMENT DATE PENDING.",
// "Code": "MP"
// },
// "Date": "19990608",
// "Time": "120000"
// }
// ],
// "PackageWeight": {
// "UnitOfMeasurement": {
// "Code": "LBS"
// },
// "Weight": "5.00"
// },
// "ReferenceNumber": [
// {
// "Code": "01",
// "Value": "LINE4AND115"
// },
// {
// "Code": "08",
// "Value": "LJ67Y5"
// }
// ]
// }
// },
// "Disclaimer": "You are using UPS tracking service on customer integration test environment, please switch to UPS production environment once you finish the test. The URL is https://onlinetools.ups.com/webservices/Track"
// }
// }
// Use the online tool at Generate JSON Parsing Code
// to generate JSON parsing code.
const status_code = try json.stringOf(alloc, "TrackResponse.Response.ResponseStatus.Code");
const status_description = try json.stringOf(alloc, "TrackResponse.Response.ResponseStatus.Description");
std.debug.print("statusCode: {s}\n", .{status_code});
std.debug.print("statusDescription{s}\n", .{status_description});
_ = try json.stringOf(alloc, "TrackResponse.Response.TransactionReference.CustomerContext");
_ = try json.stringOf(alloc, "TrackResponse.Shipment.InquiryNumber.Code");
_ = try json.stringOf(alloc, "TrackResponse.Shipment.InquiryNumber.Description");
_ = try json.stringOf(alloc, "TrackResponse.Shipment.InquiryNumber.Value");
_ = try json.stringOf(alloc, "TrackResponse.Shipment.ShipmentType.Code");
_ = try json.stringOf(alloc, "TrackResponse.Shipment.ShipmentType.Description");
_ = try json.stringOf(alloc, "TrackResponse.Shipment.ShipperNumber");
_ = try json.stringOf(alloc, "TrackResponse.Shipment.Service.Code");
_ = try json.stringOf(alloc, "TrackResponse.Shipment.Service.Description");
_ = try json.stringOf(alloc, "TrackResponse.Shipment.ReferenceNumber.Code");
_ = try json.stringOf(alloc, "TrackResponse.Shipment.ReferenceNumber.Value");
_ = try json.stringOf(alloc, "TrackResponse.Shipment.PickupDate");
const tracking_number = try json.stringOf(alloc, "TrackResponse.Shipment.Package.TrackingNumber");
_ = try json.stringOf(alloc, "TrackResponse.Shipment.Package.PackageWeight.UnitOfMeasurement.Code");
_ = try json.stringOf(alloc, "TrackResponse.Shipment.Package.PackageWeight.Weight");
_ = try json.stringOf(alloc, "TrackResponse.Disclaimer");
var i: i32 = 0;
const activity_count = json.sizeOfArray("TrackResponse.Shipment.Package.Activity");
std.debug.print("activityCount: {d}\n", .{activity_count});
while (i < activity_count) {
std.debug.print("-- activity {d}\n", .{i});
json.setI(i);
if (json.hasMember("TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.City")) {
const city = try json.stringOf(alloc, "TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.City");
std.debug.print("city: {s}\n", .{city});
_ = try json.stringOf(alloc, "TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.StateProvinceCode");
_ = try json.stringOf(alloc, "TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.PostalCode");
_ = try json.stringOf(alloc, "TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.CountryCode");
}
_ = try json.stringOf(alloc, "TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Code");
_ = try json.stringOf(alloc, "TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Description");
_ = try json.stringOf(alloc, "TrackResponse.Shipment.Package.Activity[i].ActivityLocation.SignedForByName");
const activity_status_type = try json.stringOf(alloc, "TrackResponse.Shipment.Package.Activity[i].Status.Type");
std.debug.print("activityStatusType: {s}\n", .{activity_status_type});
const activity_status_description = try json.stringOf(alloc, "TrackResponse.Shipment.Package.Activity[i].Status.Description");
std.debug.print("activityStatusDescription: {s}\n", .{activity_status_description});
const activity_status_code = try json.stringOf(alloc, "TrackResponse.Shipment.Package.Activity[i].Status.Code");
std.debug.print("activityStatusCode: {s}\n", .{activity_status_code});
_ = try json.stringOf(alloc, "TrackResponse.Shipment.Package.Activity[i].Date");
_ = try json.stringOf(alloc, "TrackResponse.Shipment.Package.Activity[i].Time");
if (json.hasMember("TrackResponse.Shipment.Package.Activity[i].Document")) {
const type_code = json.intOf("TrackResponse.Shipment.Package.Activity[i].Document.Type.Code");
_ = try json.stringOf(alloc, "TrackResponse.Shipment.Package.Activity[i].Document.Type.Description");
const document_content = try json.stringOf(alloc, "TrackResponse.Shipment.Package.Activity[i].Document.Content");
_ = try json.stringOf(alloc, "TrackResponse.Shipment.Package.Activity[i].Document.Format.Code");
// Format description would be something like "GIF" for a signature image.
const document_format_description = try json.stringOf(alloc, "TrackResponse.Shipment.Package.Activity[i].Document.Format.Description");
// 01 - Signature Image
// 02 - Delivery Receipt
// 03 - Free Astray
// 04 - POD
if (type_code == 1) {
// We have a signature image. Get the image data and save to a file.
const sb_image_path = try chilkat.StringBuilder.init();
defer sb_image_path.deinit();
sb_image_path.append("qa_output/sig_") catch {};
sb_image_path.append(tracking_number) catch {};
sb_image_path.append(".") catch {};
sb_image_path.append(document_format_description) catch {};
const image_data = try chilkat.BinData.init();
defer image_data.deinit();
try image_data.appendEncoded(document_content, "base64");
// Write to "qa_output/sig_1Z12345E0205271688.GIF"
try image_data.writeFile(try sb_image_path.getAsString(alloc));
}
}
i = i + 1;
}
i = 0;
const refnum_count = json.sizeOfArray("TrackResponse.Shipment.Package.ReferenceNumber");
while (i < refnum_count) {
json.setI(i);
_ = try json.stringOf(alloc, "TrackResponse.Shipment.Package.ReferenceNumber[i].Code");
_ = try json.stringOf(alloc, "TrackResponse.Shipment.Package.ReferenceNumber[i].Value");
i = i + 1;
}
std.debug.print("Success.\n", .{});
}