C++
C++
Shippo Adding Metadata
See more Shippo Examples
Demonstrates how to add metadata to the tracking request through a POST request.Chilkat C++ Downloads
#include <CkHttp.h>
#include <CkHttpRequest.h>
#include <CkHttpResponse.h>
#include <CkStringBuilder.h>
#include <CkJsonObject.h>
void ChilkatSample(void)
{
bool success = false;
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
CkHttp http;
// 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"
CkHttpRequest req;
req.put_HttpVerb("POST");
req.put_Path("/tracks/");
req.put_ContentType("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>");
CkHttpResponse resp;
success = http.HttpReq("https://api.goshippo.com/tracks/",req,resp);
if (success == false) {
std::cout << http.lastErrorText() << "\r\n";
return;
}
CkStringBuilder sbResponseBody;
resp.GetBodySb(sbResponseBody);
CkJsonObject jResp;
jResp.LoadSb(sbResponseBody);
jResp.put_EmitCompact(false);
std::cout << "Response Body:" << "\r\n";
std::cout << jResp.emit() << "\r\n";
int respStatusCode = resp.get_StatusCode();
std::cout << "Response Status Code = " << respStatusCode << "\r\n";
if (respStatusCode >= 400) {
std::cout << "Response Header:" << "\r\n";
std::cout << resp.header() << "\r\n";
std::cout << "Failed." << "\r\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
// Chilkat functions returning "const char *" return a pointer to temporary internal memory owned and managed by Chilkat.
// See this example explaining how this memory should be used: const char * functions.
const char *object_created = 0;
const char *object_updated = 0;
const char *object_id = 0;
const char *status = 0;
const char *status_details = 0;
const char *status_date = 0;
const char *substatus = 0;
const char *locationCity = 0;
const char *locationState = 0;
const char *locationZip = 0;
const char *locationCountry = 0;
const char *carrier = jResp.stringOf("carrier");
const char *tracking_number = jResp.stringOf("tracking_number");
const char *address_fromCity = jResp.stringOf("address_from.city");
const char *address_fromState = jResp.stringOf("address_from.state");
const char *address_fromZip = jResp.stringOf("address_from.zip");
const char *address_fromCountry = jResp.stringOf("address_from.country");
const char *address_toCity = jResp.stringOf("address_to.city");
const char *address_toState = jResp.stringOf("address_to.state");
const char *address_toZip = jResp.stringOf("address_to.zip");
const char *address_toCountry = jResp.stringOf("address_to.country");
const char *eta = jResp.stringOf("eta");
const char *original_eta = jResp.stringOf("original_eta");
const char *servicelevelToken = jResp.stringOf("servicelevel.token");
const char *servicelevelName = jResp.stringOf("servicelevel.name");
const char *metadata = jResp.stringOf("metadata");
const char *tracking_statusObject_created = jResp.stringOf("tracking_status.object_created");
const char *tracking_statusObject_updated = jResp.stringOf("tracking_status.object_updated");
const char *tracking_statusObject_id = jResp.stringOf("tracking_status.object_id");
const char *tracking_statusStatus = jResp.stringOf("tracking_status.status");
const char *tracking_statusStatus_details = jResp.stringOf("tracking_status.status_details");
const char *tracking_statusStatus_date = jResp.stringOf("tracking_status.status_date");
const char *tracking_statusSubstatus = jResp.stringOf("tracking_status.substatus");
const char *tracking_statusLocationCity = jResp.stringOf("tracking_status.location.city");
const char *tracking_statusLocationState = jResp.stringOf("tracking_status.location.state");
const char *tracking_statusLocationZip = jResp.stringOf("tracking_status.location.zip");
const char *tracking_statusLocationCountry = jResp.stringOf("tracking_status.location.country");
const char *transaction = jResp.stringOf("transaction");
bool test = jResp.BoolOf("test");
int i = 0;
int count_i = jResp.SizeOfArray("messages");
while (i < count_i) {
jResp.put_I(i);
i = i + 1;
}
i = 0;
count_i = jResp.SizeOfArray("tracking_history");
while (i < count_i) {
jResp.put_I(i);
object_created = jResp.stringOf("tracking_history[i].object_created");
object_updated = jResp.stringOf("tracking_history[i].object_updated");
object_id = jResp.stringOf("tracking_history[i].object_id");
status = jResp.stringOf("tracking_history[i].status");
status_details = jResp.stringOf("tracking_history[i].status_details");
status_date = jResp.stringOf("tracking_history[i].status_date");
substatus = jResp.stringOf("tracking_history[i].substatus");
locationCity = jResp.stringOf("tracking_history[i].location.city");
locationState = jResp.stringOf("tracking_history[i].location.state");
locationZip = jResp.stringOf("tracking_history[i].location.zip");
locationCountry = jResp.stringOf("tracking_history[i].location.country");
i = i + 1;
}
}