Unicode C
Unicode C
Shippo Create a Batch
See more Shippo Examples
Demonstrates how to create and purchase up to 10,000 shipments in a single API request.Chilkat Unicode C Downloads
#include <C_CkHttpW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkHttpResponseW.h>
#include <C_CkStringBuilderW.h>
void ChilkatSample(void)
{
BOOL success;
HCkHttpW http;
HCkJsonObjectW json;
HCkHttpResponseW resp;
HCkStringBuilderW sbResponseBody;
HCkJsonObjectW jResp;
int respStatusCode;
const wchar_t *object_id;
const wchar_t *object_owner;
const wchar_t *status;
const wchar_t *object_created;
const wchar_t *object_updated;
const wchar_t *metadata;
const wchar_t *default_carrier_account;
const wchar_t *default_servicelevel_token;
const wchar_t *label_filetype;
const wchar_t *batch_shipmentsNext;
const wchar_t *batch_shipmentsPrevious;
int object_resultsPurchase_succeeded;
int object_resultsPurchase_failed;
int object_resultsCreation_failed;
int object_resultsCreation_succeeded;
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 = CkHttpW_Create();
// Use this online tool to generate code from sample JSON:
// Generate Code to Create JSON
// The following JSON is sent in the request body.
// {
// "default_carrier_account": "078870331023437cb917f5187429b093",
// "default_servicelevel_token": "usps_priority",
// "label_filetype": "PDF_4x6",
// "metadata": "BATCH #170",
// "batch_shipments": [
// {
// "shipment": {
// "address_from": {
// "name": "Mr Hippo",
// "street1": "965 Mission St",
// "street2": "Ste 201",
// "city": "San Francisco",
// "state": "CA",
// "zip": "94103",
// "country": "US",
// "phone": "4151234567",
// "email": "mrhippo@goshippo.com"
// },
// "address_to": {
// "name": "Mrs Hippo",
// "company": "",
// "street1": "Broadway 1",
// "street2": "",
// "city": "New York",
// "state": "NY",
// "zip": "10007",
// "country": "US",
// "phone": "4151234567",
// "email": "mrshippo@goshippo.com"
// },
// "parcels": [
// {
// "length": "5",
// "width": "5",
// "height": "5",
// "distance_unit": "in",
// "weight": "2",
// "mass_unit": "oz"
// }
// ]
// }
// },
// {
// "shipment": {
// "address_from": {
// "name": "Mr Hippo",
// "street1": "1092 Indian Summer Ct",
// "city": "San Jose",
// "state": "CA",
// "zip": "95122",
// "country": "US",
// "phone": "4151234567",
// "email": "mrhippo@goshippo.com"
// },
// "address_to": {
// "name": "Mrs Hippo",
// "company": "",
// "street1": "Broadway 1",
// "street2": "",
// "city": "New York",
// "state": "NY",
// "zip": "10007",
// "country": "US",
// "phone": "4151234567",
// "email": "mrshippo@goshippo.com"
// },
// "parcels": [
// {
// "length": "5",
// "width": "5",
// "height": "5",
// "distance_unit": "in",
// "weight": "20",
// "mass_unit": "lb"
// }
// ]
// },
// "carrier_account": "a4391cd4ab974f478f55dc08b5c8e3b3",
// "servicelevel_token": "fedex_2_day"
// }
// ]
// }
json = CkJsonObjectW_Create();
CkJsonObjectW_UpdateString(json,L"default_carrier_account",L"078870331023437cb917f5187429b093");
CkJsonObjectW_UpdateString(json,L"default_servicelevel_token",L"usps_priority");
CkJsonObjectW_UpdateString(json,L"label_filetype",L"PDF_4x6");
CkJsonObjectW_UpdateString(json,L"metadata",L"BATCH #170");
CkJsonObjectW_UpdateString(json,L"batch_shipments[0].shipment.address_from.name",L"Mr Hippo");
CkJsonObjectW_UpdateString(json,L"batch_shipments[0].shipment.address_from.street1",L"965 Mission St");
CkJsonObjectW_UpdateString(json,L"batch_shipments[0].shipment.address_from.street2",L"Ste 201");
CkJsonObjectW_UpdateString(json,L"batch_shipments[0].shipment.address_from.city",L"San Francisco");
CkJsonObjectW_UpdateString(json,L"batch_shipments[0].shipment.address_from.state",L"CA");
CkJsonObjectW_UpdateString(json,L"batch_shipments[0].shipment.address_from.zip",L"94103");
CkJsonObjectW_UpdateString(json,L"batch_shipments[0].shipment.address_from.country",L"US");
CkJsonObjectW_UpdateString(json,L"batch_shipments[0].shipment.address_from.phone",L"4151234567");
CkJsonObjectW_UpdateString(json,L"batch_shipments[0].shipment.address_from.email",L"mrhippo@goshippo.com");
CkJsonObjectW_UpdateString(json,L"batch_shipments[0].shipment.address_to.name",L"Mrs Hippo");
CkJsonObjectW_UpdateString(json,L"batch_shipments[0].shipment.address_to.company",L"");
CkJsonObjectW_UpdateString(json,L"batch_shipments[0].shipment.address_to.street1",L"Broadway 1");
CkJsonObjectW_UpdateString(json,L"batch_shipments[0].shipment.address_to.street2",L"");
CkJsonObjectW_UpdateString(json,L"batch_shipments[0].shipment.address_to.city",L"New York");
CkJsonObjectW_UpdateString(json,L"batch_shipments[0].shipment.address_to.state",L"NY");
CkJsonObjectW_UpdateString(json,L"batch_shipments[0].shipment.address_to.zip",L"10007");
CkJsonObjectW_UpdateString(json,L"batch_shipments[0].shipment.address_to.country",L"US");
CkJsonObjectW_UpdateString(json,L"batch_shipments[0].shipment.address_to.phone",L"4151234567");
CkJsonObjectW_UpdateString(json,L"batch_shipments[0].shipment.address_to.email",L"mrshippo@goshippo.com");
CkJsonObjectW_UpdateString(json,L"batch_shipments[0].shipment.parcels[0].length",L"5");
CkJsonObjectW_UpdateString(json,L"batch_shipments[0].shipment.parcels[0].width",L"5");
CkJsonObjectW_UpdateString(json,L"batch_shipments[0].shipment.parcels[0].height",L"5");
CkJsonObjectW_UpdateString(json,L"batch_shipments[0].shipment.parcels[0].distance_unit",L"in");
CkJsonObjectW_UpdateString(json,L"batch_shipments[0].shipment.parcels[0].weight",L"2");
CkJsonObjectW_UpdateString(json,L"batch_shipments[0].shipment.parcels[0].mass_unit",L"oz");
CkJsonObjectW_UpdateString(json,L"batch_shipments[1].shipment.address_from.name",L"Mr Hippo");
CkJsonObjectW_UpdateString(json,L"batch_shipments[1].shipment.address_from.street1",L"1092 Indian Summer Ct");
CkJsonObjectW_UpdateString(json,L"batch_shipments[1].shipment.address_from.city",L"San Jose");
CkJsonObjectW_UpdateString(json,L"batch_shipments[1].shipment.address_from.state",L"CA");
CkJsonObjectW_UpdateString(json,L"batch_shipments[1].shipment.address_from.zip",L"95122");
CkJsonObjectW_UpdateString(json,L"batch_shipments[1].shipment.address_from.country",L"US");
CkJsonObjectW_UpdateString(json,L"batch_shipments[1].shipment.address_from.phone",L"4151234567");
CkJsonObjectW_UpdateString(json,L"batch_shipments[1].shipment.address_from.email",L"mrhippo@goshippo.com");
CkJsonObjectW_UpdateString(json,L"batch_shipments[1].shipment.address_to.name",L"Mrs Hippo");
CkJsonObjectW_UpdateString(json,L"batch_shipments[1].shipment.address_to.company",L"");
CkJsonObjectW_UpdateString(json,L"batch_shipments[1].shipment.address_to.street1",L"Broadway 1");
CkJsonObjectW_UpdateString(json,L"batch_shipments[1].shipment.address_to.street2",L"");
CkJsonObjectW_UpdateString(json,L"batch_shipments[1].shipment.address_to.city",L"New York");
CkJsonObjectW_UpdateString(json,L"batch_shipments[1].shipment.address_to.state",L"NY");
CkJsonObjectW_UpdateString(json,L"batch_shipments[1].shipment.address_to.zip",L"10007");
CkJsonObjectW_UpdateString(json,L"batch_shipments[1].shipment.address_to.country",L"US");
CkJsonObjectW_UpdateString(json,L"batch_shipments[1].shipment.address_to.phone",L"4151234567");
CkJsonObjectW_UpdateString(json,L"batch_shipments[1].shipment.address_to.email",L"mrshippo@goshippo.com");
CkJsonObjectW_UpdateString(json,L"batch_shipments[1].shipment.parcels[0].length",L"5");
CkJsonObjectW_UpdateString(json,L"batch_shipments[1].shipment.parcels[0].width",L"5");
CkJsonObjectW_UpdateString(json,L"batch_shipments[1].shipment.parcels[0].height",L"5");
CkJsonObjectW_UpdateString(json,L"batch_shipments[1].shipment.parcels[0].distance_unit",L"in");
CkJsonObjectW_UpdateString(json,L"batch_shipments[1].shipment.parcels[0].weight",L"20");
CkJsonObjectW_UpdateString(json,L"batch_shipments[1].shipment.parcels[0].mass_unit",L"lb");
CkJsonObjectW_UpdateString(json,L"batch_shipments[1].carrier_account",L"a4391cd4ab974f478f55dc08b5c8e3b3");
CkJsonObjectW_UpdateString(json,L"batch_shipments[1].servicelevel_token",L"fedex_2_day");
CkHttpW_SetRequestHeader(http,L"Authorization",L"ShippoToken shippo_test_3af5d574e2f845d30efcaf9b2f47d9c2aef4807a");
CkHttpW_SetRequestHeader(http,L"Content-Type",L"application/json");
resp = CkHttpResponseW_Create();
success = CkHttpW_HttpJson(http,L"POST",L"https://api.goshippo.com/batches/",json,L"application/json",resp);
if (success == FALSE) {
wprintf(L"%s\n",CkHttpW_lastErrorText(http));
CkHttpW_Dispose(http);
CkJsonObjectW_Dispose(json);
CkHttpResponseW_Dispose(resp);
return;
}
sbResponseBody = CkStringBuilderW_Create();
CkHttpResponseW_GetBodySb(resp,sbResponseBody);
jResp = CkJsonObjectW_Create();
CkJsonObjectW_LoadSb(jResp,sbResponseBody);
CkJsonObjectW_putEmitCompact(jResp,FALSE);
wprintf(L"Response Body:\n");
wprintf(L"%s\n",CkJsonObjectW_emit(jResp));
respStatusCode = CkHttpResponseW_getStatusCode(resp);
wprintf(L"Response Status Code = %d\n",respStatusCode);
if (respStatusCode >= 400) {
wprintf(L"Response Header:\n");
wprintf(L"%s\n",CkHttpResponseW_header(resp));
wprintf(L"Failed.\n");
CkHttpW_Dispose(http);
CkJsonObjectW_Dispose(json);
CkHttpResponseW_Dispose(resp);
CkStringBuilderW_Dispose(sbResponseBody);
CkJsonObjectW_Dispose(jResp);
return;
}
// Sample JSON response:
// (Sample code for parsing the JSON response is shown below)
// {
// "object_id": "a015eb693cca465dbb6523ce6d2e3c65",
// "object_owner": "admin",
// "status": "VALIDATING",
// "object_created": "2016-09-12T15:25:43.465Z",
// "object_updated": "2016-09-12T15:25:43.465Z",
// "metadata": "BATCH #170",
// "default_carrier_account": "078870331023437cb917f5187429b093",
// "default_servicelevel_token": "usps_priority",
// "label_filetype": "PDF_4x6",
// "batch_shipments": {
// "next": null,
// "previous": null,
// "results": [
// ]
// },
// "object_results": {
// "purchase_succeeded": 0,
// "purchase_failed": 0,
// "creation_failed": 0,
// "creation_succeeded": 0
// },
// "label_url": [
// ]
// }
// 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.
object_id = CkJsonObjectW_stringOf(jResp,L"object_id");
object_owner = CkJsonObjectW_stringOf(jResp,L"object_owner");
status = CkJsonObjectW_stringOf(jResp,L"status");
object_created = CkJsonObjectW_stringOf(jResp,L"object_created");
object_updated = CkJsonObjectW_stringOf(jResp,L"object_updated");
metadata = CkJsonObjectW_stringOf(jResp,L"metadata");
default_carrier_account = CkJsonObjectW_stringOf(jResp,L"default_carrier_account");
default_servicelevel_token = CkJsonObjectW_stringOf(jResp,L"default_servicelevel_token");
label_filetype = CkJsonObjectW_stringOf(jResp,L"label_filetype");
batch_shipmentsNext = CkJsonObjectW_stringOf(jResp,L"batch_shipments.next");
batch_shipmentsPrevious = CkJsonObjectW_stringOf(jResp,L"batch_shipments.previous");
object_resultsPurchase_succeeded = CkJsonObjectW_IntOf(jResp,L"object_results.purchase_succeeded");
object_resultsPurchase_failed = CkJsonObjectW_IntOf(jResp,L"object_results.purchase_failed");
object_resultsCreation_failed = CkJsonObjectW_IntOf(jResp,L"object_results.creation_failed");
object_resultsCreation_succeeded = CkJsonObjectW_IntOf(jResp,L"object_results.creation_succeeded");
i = 0;
count_i = CkJsonObjectW_SizeOfArray(jResp,L"batch_shipments.results");
while (i < count_i) {
CkJsonObjectW_putI(jResp,i);
i = i + 1;
}
i = 0;
count_i = CkJsonObjectW_SizeOfArray(jResp,L"label_url");
while (i < count_i) {
CkJsonObjectW_putI(jResp,i);
i = i + 1;
}
CkHttpW_Dispose(http);
CkJsonObjectW_Dispose(json);
CkHttpResponseW_Dispose(resp);
CkStringBuilderW_Dispose(sbResponseBody);
CkJsonObjectW_Dispose(jResp);
}