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 <C_CkHttp.h>
#include <C_CkHttpRequest.h>
#include <C_CkHttpResponse.h>
#include <C_CkStringBuilder.h>
#include <C_CkJsonObject.h>
void ChilkatSample(void)
{
BOOL success;
HCkHttp http;
HCkHttpRequest req;
HCkHttpResponse resp;
HCkStringBuilder sbResponseBody;
HCkJsonObject jResp;
int respStatusCode;
const char *object_created;
const char *object_updated;
const char *object_id;
const char *status;
const char *status_details;
const char *status_date;
const char *substatus;
const char *locationCity;
const char *locationState;
const char *locationZip;
const char *locationCountry;
const char *carrier;
const char *tracking_number;
const char *address_fromCity;
const char *address_fromState;
const char *address_fromZip;
const char *address_fromCountry;
const char *address_toCity;
const char *address_toState;
const char *address_toZip;
const char *address_toCountry;
const char *eta;
const char *original_eta;
const char *servicelevelToken;
const char *servicelevelName;
const char *metadata;
const char *tracking_statusObject_created;
const char *tracking_statusObject_updated;
const char *tracking_statusObject_id;
const char *tracking_statusStatus;
const char *tracking_statusStatus_details;
const char *tracking_statusStatus_date;
const char *tracking_statusSubstatus;
const char *tracking_statusLocationCity;
const char *tracking_statusLocationState;
const char *tracking_statusLocationZip;
const char *tracking_statusLocationCountry;
const char *transaction;
BOOL test;
int i;
int count_i;
success = FALSE;
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
http = CkHttp_Create();
// 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"
req = CkHttpRequest_Create();
CkHttpRequest_putHttpVerb(req,"POST");
CkHttpRequest_putPath(req,"/tracks/");
CkHttpRequest_putContentType(req,"application/x-www-form-urlencoded");
CkHttpRequest_AddParam(req,"carrier","shippo");
CkHttpRequest_AddParam(req,"tracking_number","SHIPPO_TRANSIT");
CkHttpRequest_AddParam(req,"metadata","Order 000123");
CkHttpRequest_AddHeader(req,"Authorization","ShippoToken <API_TOKEN>");
resp = CkHttpResponse_Create();
success = CkHttp_HttpReq(http,"https://api.goshippo.com/tracks/",req,resp);
if (success == FALSE) {
printf("%s\n",CkHttp_lastErrorText(http));
CkHttp_Dispose(http);
CkHttpRequest_Dispose(req);
CkHttpResponse_Dispose(resp);
return;
}
sbResponseBody = CkStringBuilder_Create();
CkHttpResponse_GetBodySb(resp,sbResponseBody);
jResp = CkJsonObject_Create();
CkJsonObject_LoadSb(jResp,sbResponseBody);
CkJsonObject_putEmitCompact(jResp,FALSE);
printf("Response Body:\n");
printf("%s\n",CkJsonObject_emit(jResp));
respStatusCode = CkHttpResponse_getStatusCode(resp);
printf("Response Status Code = %d\n",respStatusCode);
if (respStatusCode >= 400) {
printf("Response Header:\n");
printf("%s\n",CkHttpResponse_header(resp));
printf("Failed.\n");
CkHttp_Dispose(http);
CkHttpRequest_Dispose(req);
CkHttpResponse_Dispose(resp);
CkStringBuilder_Dispose(sbResponseBody);
CkJsonObject_Dispose(jResp);
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.
carrier = CkJsonObject_stringOf(jResp,"carrier");
tracking_number = CkJsonObject_stringOf(jResp,"tracking_number");
address_fromCity = CkJsonObject_stringOf(jResp,"address_from.city");
address_fromState = CkJsonObject_stringOf(jResp,"address_from.state");
address_fromZip = CkJsonObject_stringOf(jResp,"address_from.zip");
address_fromCountry = CkJsonObject_stringOf(jResp,"address_from.country");
address_toCity = CkJsonObject_stringOf(jResp,"address_to.city");
address_toState = CkJsonObject_stringOf(jResp,"address_to.state");
address_toZip = CkJsonObject_stringOf(jResp,"address_to.zip");
address_toCountry = CkJsonObject_stringOf(jResp,"address_to.country");
eta = CkJsonObject_stringOf(jResp,"eta");
original_eta = CkJsonObject_stringOf(jResp,"original_eta");
servicelevelToken = CkJsonObject_stringOf(jResp,"servicelevel.token");
servicelevelName = CkJsonObject_stringOf(jResp,"servicelevel.name");
metadata = CkJsonObject_stringOf(jResp,"metadata");
tracking_statusObject_created = CkJsonObject_stringOf(jResp,"tracking_status.object_created");
tracking_statusObject_updated = CkJsonObject_stringOf(jResp,"tracking_status.object_updated");
tracking_statusObject_id = CkJsonObject_stringOf(jResp,"tracking_status.object_id");
tracking_statusStatus = CkJsonObject_stringOf(jResp,"tracking_status.status");
tracking_statusStatus_details = CkJsonObject_stringOf(jResp,"tracking_status.status_details");
tracking_statusStatus_date = CkJsonObject_stringOf(jResp,"tracking_status.status_date");
tracking_statusSubstatus = CkJsonObject_stringOf(jResp,"tracking_status.substatus");
tracking_statusLocationCity = CkJsonObject_stringOf(jResp,"tracking_status.location.city");
tracking_statusLocationState = CkJsonObject_stringOf(jResp,"tracking_status.location.state");
tracking_statusLocationZip = CkJsonObject_stringOf(jResp,"tracking_status.location.zip");
tracking_statusLocationCountry = CkJsonObject_stringOf(jResp,"tracking_status.location.country");
transaction = CkJsonObject_stringOf(jResp,"transaction");
test = CkJsonObject_BoolOf(jResp,"test");
i = 0;
count_i = CkJsonObject_SizeOfArray(jResp,"messages");
while (i < count_i) {
CkJsonObject_putI(jResp,i);
i = i + 1;
}
i = 0;
count_i = CkJsonObject_SizeOfArray(jResp,"tracking_history");
while (i < count_i) {
CkJsonObject_putI(jResp,i);
object_created = CkJsonObject_stringOf(jResp,"tracking_history[i].object_created");
object_updated = CkJsonObject_stringOf(jResp,"tracking_history[i].object_updated");
object_id = CkJsonObject_stringOf(jResp,"tracking_history[i].object_id");
status = CkJsonObject_stringOf(jResp,"tracking_history[i].status");
status_details = CkJsonObject_stringOf(jResp,"tracking_history[i].status_details");
status_date = CkJsonObject_stringOf(jResp,"tracking_history[i].status_date");
substatus = CkJsonObject_stringOf(jResp,"tracking_history[i].substatus");
locationCity = CkJsonObject_stringOf(jResp,"tracking_history[i].location.city");
locationState = CkJsonObject_stringOf(jResp,"tracking_history[i].location.state");
locationZip = CkJsonObject_stringOf(jResp,"tracking_history[i].location.zip");
locationCountry = CkJsonObject_stringOf(jResp,"tracking_history[i].location.country");
i = i + 1;
}
CkHttp_Dispose(http);
CkHttpRequest_Dispose(req);
CkHttpResponse_Dispose(resp);
CkStringBuilder_Dispose(sbResponseBody);
CkJsonObject_Dispose(jResp);
}