Zig
Zig
Shippo Adding Metadata
See more Shippo Examples
Demonstrates how to add metadata to the tracking request through a POST request.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/tracks/ \
// -H "Authorization: ShippoToken <API_TOKEN>" \
// -d carrier="shippo" \
// -d tracking_number="SHIPPO_TRANSIT" \
// -d metadata="Order 000123"
const req = try chilkat.HttpRequest.init();
defer req.deinit();
req.setHttpVerb("POST");
req.setPath("/tracks/");
req.setContentType("application/x-www-form-urlencoded");
req.addParam("carrier", "shippo");
req.addParam("tracking_number", "SHIPPO_TRANSIT");
req.addParam("metadata", "Order 000123");
req.addHeader("Authorization", "ShippoToken <API_TOKEN>");
const resp = try chilkat.HttpResponse.init();
defer resp.deinit();
http.httpReq("https://api.goshippo.com/tracks/", 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)
// {
// "messages": [
// ],
// "carrier": "shippo",
// "tracking_number": "SHIPPO_TRANSIT",
// "address_from": {
// "city": "San Francisco",
// "state": "CA",
// "zip": "94103",
// "country": "US"
// },
// "address_to": {
// "city": "Chicago",
// "state": "IL",
// "zip": "60611",
// "country": "US"
// },
// "eta": "2019-07-07T17:07:44.989Z",
// "original_eta": "2019-07-07T17:07:44.989Z",
// "servicelevel": {
// "token": "shippo_priority",
// "name": "Priority Mail"
// },
// "metadata": "Shippo test tracking",
// "tracking_status": {
// "object_created": "2019-07-04T17:07:45.003Z",
// "object_updated": null,
// "object_id": "ee35fb56f5d04021b36168abedc04573",
// "status": "TRANSIT",
// "status_details": "Your shipment has departed from the origin.",
// "status_date": "2019-07-03T15:02:45.003Z",
// "substatus": null,
// "location": {
// "city": "San Francisco",
// "state": "CA",
// "zip": "94103",
// "country": "US"
// }
// },
// "tracking_history": [
// {
// "object_created": "2019-07-04T17:07:45.005Z",
// "object_updated": null,
// "object_id": "2121a59f53ed42e0ae0436f636179156",
// "status": "UNKNOWN",
// "status_details": "The carrier has received the electronic shipment information.",
// "status_date": "2019-07-02T12:57:45.005Z",
// "substatus": null,
// "location": {
// "city": "San Francisco",
// "state": "CA",
// "zip": "94103",
// "country": "US"
// }
// },
// {
// "object_created": "2019-07-04T17:07:45.005Z",
// "object_updated": null,
// "object_id": "06f949db1a8245beaa28df264b368a76",
// "status": "TRANSIT",
// "status_details": "Your shipment has departed from the origin.",
// "status_date": "2019-07-03T15:02:45.005Z",
// "substatus": null,
// "location": {
// "city": "San Francisco",
// "state": "CA",
// "zip": "94103",
// "country": "US"
// }
// }
// ],
// "transaction": null,
// "test": true
// }
// 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 object_created: [:0]const u8 = "";
var object_updated: [:0]const u8 = "";
var object_id: [:0]const u8 = "";
var status: [:0]const u8 = "";
var status_details: [:0]const u8 = "";
var status_date: [:0]const u8 = "";
var substatus: [:0]const u8 = "";
var location_city: [:0]const u8 = "";
var location_state: [:0]const u8 = "";
var location_zip: [:0]const u8 = "";
var location_country: [:0]const u8 = "";
_ = try j_resp.stringOf(alloc, "carrier");
_ = try j_resp.stringOf(alloc, "tracking_number");
_ = try j_resp.stringOf(alloc, "address_from.city");
_ = try j_resp.stringOf(alloc, "address_from.state");
_ = try j_resp.stringOf(alloc, "address_from.zip");
_ = try j_resp.stringOf(alloc, "address_from.country");
_ = try j_resp.stringOf(alloc, "address_to.city");
_ = try j_resp.stringOf(alloc, "address_to.state");
_ = try j_resp.stringOf(alloc, "address_to.zip");
_ = try j_resp.stringOf(alloc, "address_to.country");
_ = try j_resp.stringOf(alloc, "eta");
_ = try j_resp.stringOf(alloc, "original_eta");
_ = try j_resp.stringOf(alloc, "servicelevel.token");
_ = try j_resp.stringOf(alloc, "servicelevel.name");
_ = try j_resp.stringOf(alloc, "metadata");
_ = try j_resp.stringOf(alloc, "tracking_status.object_created");
_ = try j_resp.stringOf(alloc, "tracking_status.object_updated");
_ = try j_resp.stringOf(alloc, "tracking_status.object_id");
_ = try j_resp.stringOf(alloc, "tracking_status.status");
_ = try j_resp.stringOf(alloc, "tracking_status.status_details");
_ = try j_resp.stringOf(alloc, "tracking_status.status_date");
_ = try j_resp.stringOf(alloc, "tracking_status.substatus");
_ = try j_resp.stringOf(alloc, "tracking_status.location.city");
_ = try j_resp.stringOf(alloc, "tracking_status.location.state");
_ = try j_resp.stringOf(alloc, "tracking_status.location.zip");
_ = try j_resp.stringOf(alloc, "tracking_status.location.country");
_ = try j_resp.stringOf(alloc, "transaction");
_ = j_resp.boolOf("test");
var i: i32 = 0;
var count_i: i32 = j_resp.sizeOfArray("messages");
while (i < count_i) {
j_resp.setI(i);
i = i + 1;
}
i = 0;
count_i = j_resp.sizeOfArray("tracking_history");
while (i < count_i) {
j_resp.setI(i);
object_created = try j_resp.stringOf(alloc, "tracking_history[i].object_created");
object_updated = try j_resp.stringOf(alloc, "tracking_history[i].object_updated");
object_id = try j_resp.stringOf(alloc, "tracking_history[i].object_id");
status = try j_resp.stringOf(alloc, "tracking_history[i].status");
status_details = try j_resp.stringOf(alloc, "tracking_history[i].status_details");
status_date = try j_resp.stringOf(alloc, "tracking_history[i].status_date");
substatus = try j_resp.stringOf(alloc, "tracking_history[i].substatus");
location_city = try j_resp.stringOf(alloc, "tracking_history[i].location.city");
location_state = try j_resp.stringOf(alloc, "tracking_history[i].location.state");
location_zip = try j_resp.stringOf(alloc, "tracking_history[i].location.zip");
location_country = try j_resp.stringOf(alloc, "tracking_history[i].location.country");
i = i + 1;
}
}