Unicode C
Unicode C
Shippo Create Customs Declaration
See more Shippo Examples
Demonstrates how to create a customs declaration and send a POST request with the necessary information to the Customs Declarations endpoint.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 *strVal;
const wchar_t *object_created;
const wchar_t *object_updated;
const wchar_t *object_id;
const wchar_t *object_owner;
const wchar_t *object_state;
const wchar_t *address_importer;
const wchar_t *certify_signer;
BOOL certify;
const wchar_t *non_delivery_option;
const wchar_t *contents_type;
const wchar_t *contents_explanation;
const wchar_t *exporter_reference;
const wchar_t *importer_reference;
const wchar_t *invoice;
BOOL commercial_invoice;
const wchar_t *license;
const wchar_t *certificate;
const wchar_t *notes;
const wchar_t *eel_pfc;
const wchar_t *aes_itn;
const wchar_t *disclaimer;
const wchar_t *incoterm;
const wchar_t *metadata;
BOOL test;
const wchar_t *duties_payor;
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();
// Implements the following CURL command:
// curl https://api.goshippo.com/customs/declarations/ \
// -H "Authorization: ShippoToken <API_TOKEN>" \
// -H "Content-Type: application/json" \
// -d '{
// "contents_type": "MERCHANDISE",
// "non_delivery_option": "RETURN",
// "certify": true,
// "certify_signer": "Simon Kreuz",
// "incoterm": "DDU",
// "items": [{
// "description": "T-shirt",
// "quantity": 20,
// "net_weight": "5",
// "mass_unit": "lb",
// "value_amount": "200",
// "value_currency": "USD",
// "tariff_number": "",
// "origin_country": "US"
// }]
// }'
// Use this online tool to generate code from sample JSON:
// Generate Code to Create JSON
// The following JSON is sent in the request body.
// {
// "contents_type": "MERCHANDISE",
// "non_delivery_option": "RETURN",
// "certify": true,
// "certify_signer": "Simon Kreuz",
// "incoterm": "DDU",
// "items": [
// {
// "description": "T-shirt",
// "quantity": 20,
// "net_weight": "5",
// "mass_unit": "lb",
// "value_amount": "200",
// "value_currency": "USD",
// "tariff_number": "",
// "origin_country": "US"
// }
// ]
// }
json = CkJsonObjectW_Create();
CkJsonObjectW_UpdateString(json,L"contents_type",L"MERCHANDISE");
CkJsonObjectW_UpdateString(json,L"non_delivery_option",L"RETURN");
CkJsonObjectW_UpdateBool(json,L"certify",TRUE);
CkJsonObjectW_UpdateString(json,L"certify_signer",L"Simon Kreuz");
CkJsonObjectW_UpdateString(json,L"incoterm",L"DDU");
CkJsonObjectW_UpdateString(json,L"items[0].description",L"T-shirt");
CkJsonObjectW_UpdateInt(json,L"items[0].quantity",20);
CkJsonObjectW_UpdateString(json,L"items[0].net_weight",L"5");
CkJsonObjectW_UpdateString(json,L"items[0].mass_unit",L"lb");
CkJsonObjectW_UpdateString(json,L"items[0].value_amount",L"200");
CkJsonObjectW_UpdateString(json,L"items[0].value_currency",L"USD");
CkJsonObjectW_UpdateString(json,L"items[0].tariff_number",L"");
CkJsonObjectW_UpdateString(json,L"items[0].origin_country",L"US");
CkHttpW_SetRequestHeader(http,L"Authorization",L"ShippoToken <API_TOKEN>");
CkHttpW_SetRequestHeader(http,L"Content-Type",L"application/json");
resp = CkHttpResponseW_Create();
success = CkHttpW_HttpJson(http,L"POST",L"https://api.goshippo.com/customs/declarations/",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_created": "2019-07-04T16:00:29.043Z",
// "object_updated": "2019-07-04T16:00:29.043Z",
// "object_id": "4a1e6ab7b1ba49ed9bc6cb1a8798e0fd",
// "object_owner": "admin@chilkatsoft.com",
// "object_state": "VALID",
// "address_importer": null,
// "certify_signer": "Simon Kreuz",
// "certify": true,
// "items": [
// "4096c68693364b7ea0af72fb869ee861"
// ],
// "non_delivery_option": "RETURN",
// "contents_type": "MERCHANDISE",
// "contents_explanation": "",
// "exporter_reference": "",
// "importer_reference": "",
// "invoice": "",
// "commercial_invoice": false,
// "license": "",
// "certificate": "",
// "notes": "",
// "eel_pfc": "",
// "aes_itn": "",
// "disclaimer": "",
// "incoterm": "DDU",
// "metadata": "",
// "test": true,
// "duties_payor": null
// }
// 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_created = CkJsonObjectW_stringOf(jResp,L"object_created");
object_updated = CkJsonObjectW_stringOf(jResp,L"object_updated");
object_id = CkJsonObjectW_stringOf(jResp,L"object_id");
object_owner = CkJsonObjectW_stringOf(jResp,L"object_owner");
object_state = CkJsonObjectW_stringOf(jResp,L"object_state");
address_importer = CkJsonObjectW_stringOf(jResp,L"address_importer");
certify_signer = CkJsonObjectW_stringOf(jResp,L"certify_signer");
certify = CkJsonObjectW_BoolOf(jResp,L"certify");
non_delivery_option = CkJsonObjectW_stringOf(jResp,L"non_delivery_option");
contents_type = CkJsonObjectW_stringOf(jResp,L"contents_type");
contents_explanation = CkJsonObjectW_stringOf(jResp,L"contents_explanation");
exporter_reference = CkJsonObjectW_stringOf(jResp,L"exporter_reference");
importer_reference = CkJsonObjectW_stringOf(jResp,L"importer_reference");
invoice = CkJsonObjectW_stringOf(jResp,L"invoice");
commercial_invoice = CkJsonObjectW_BoolOf(jResp,L"commercial_invoice");
license = CkJsonObjectW_stringOf(jResp,L"license");
certificate = CkJsonObjectW_stringOf(jResp,L"certificate");
notes = CkJsonObjectW_stringOf(jResp,L"notes");
eel_pfc = CkJsonObjectW_stringOf(jResp,L"eel_pfc");
aes_itn = CkJsonObjectW_stringOf(jResp,L"aes_itn");
disclaimer = CkJsonObjectW_stringOf(jResp,L"disclaimer");
incoterm = CkJsonObjectW_stringOf(jResp,L"incoterm");
metadata = CkJsonObjectW_stringOf(jResp,L"metadata");
test = CkJsonObjectW_BoolOf(jResp,L"test");
duties_payor = CkJsonObjectW_stringOf(jResp,L"duties_payor");
i = 0;
count_i = CkJsonObjectW_SizeOfArray(jResp,L"items");
while (i < count_i) {
CkJsonObjectW_putI(jResp,i);
strVal = CkJsonObjectW_stringOf(jResp,L"items[i]");
i = i + 1;
}
CkHttpW_Dispose(http);
CkJsonObjectW_Dispose(json);
CkHttpResponseW_Dispose(resp);
CkStringBuilderW_Dispose(sbResponseBody);
CkJsonObjectW_Dispose(jResp);
}