Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(C) Shippo Create Customs DeclarationDemonstrates how to create a customs declaration and send a POST request with the necessary information to the Customs Declarations endpoint. For more information, see https://goshippo.com/docs/international/
#include <C_CkHttp.h> #include <C_CkJsonObject.h> #include <C_CkHttpResponse.h> #include <C_CkStringBuilder.h> void ChilkatSample(void) { HCkHttp http; BOOL success; HCkJsonObject json; HCkHttpResponse resp; HCkStringBuilder sbResponseBody; HCkJsonObject jResp; int respStatusCode; const char *strVal; const char *object_created; const char *object_updated; const char *object_id; const char *object_owner; const char *object_state; const char *address_importer; const char *certify_signer; BOOL certify; const char *non_delivery_option; const char *contents_type; const char *contents_explanation; const char *exporter_reference; const char *importer_reference; const char *invoice; BOOL commercial_invoice; const char *license; const char *certificate; const char *notes; const char *eel_pfc; const char *aes_itn; const char *disclaimer; const char *incoterm; const char *metadata; BOOL test; const char *duties_payor; int i; int count_i; // 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/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 = CkJsonObject_Create(); CkJsonObject_UpdateString(json,"contents_type","MERCHANDISE"); CkJsonObject_UpdateString(json,"non_delivery_option","RETURN"); CkJsonObject_UpdateBool(json,"certify",TRUE); CkJsonObject_UpdateString(json,"certify_signer","Simon Kreuz"); CkJsonObject_UpdateString(json,"incoterm","DDU"); CkJsonObject_UpdateString(json,"items[0].description","T-shirt"); CkJsonObject_UpdateInt(json,"items[0].quantity",20); CkJsonObject_UpdateString(json,"items[0].net_weight","5"); CkJsonObject_UpdateString(json,"items[0].mass_unit","lb"); CkJsonObject_UpdateString(json,"items[0].value_amount","200"); CkJsonObject_UpdateString(json,"items[0].value_currency","USD"); CkJsonObject_UpdateString(json,"items[0].tariff_number",""); CkJsonObject_UpdateString(json,"items[0].origin_country","US"); CkHttp_SetRequestHeader(http,"Authorization","ShippoToken <API_TOKEN>"); CkHttp_SetRequestHeader(http,"Content-Type","application/json"); resp = CkHttp_PostJson3(http,"https://api.goshippo.com/customs/declarations/","application/json",json); if (CkHttp_getLastMethodSuccess(http) == FALSE) { printf("%s\n",CkHttp_lastErrorText(http)); CkHttp_Dispose(http); CkJsonObject_Dispose(json); 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"); CkHttpResponse_Dispose(resp); CkHttp_Dispose(http); CkJsonObject_Dispose(json); CkStringBuilder_Dispose(sbResponseBody); CkJsonObject_Dispose(jResp); return; } CkHttpResponse_Dispose(resp); // 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 = CkJsonObject_stringOf(jResp,"object_created"); object_updated = CkJsonObject_stringOf(jResp,"object_updated"); object_id = CkJsonObject_stringOf(jResp,"object_id"); object_owner = CkJsonObject_stringOf(jResp,"object_owner"); object_state = CkJsonObject_stringOf(jResp,"object_state"); address_importer = CkJsonObject_stringOf(jResp,"address_importer"); certify_signer = CkJsonObject_stringOf(jResp,"certify_signer"); certify = CkJsonObject_BoolOf(jResp,"certify"); non_delivery_option = CkJsonObject_stringOf(jResp,"non_delivery_option"); contents_type = CkJsonObject_stringOf(jResp,"contents_type"); contents_explanation = CkJsonObject_stringOf(jResp,"contents_explanation"); exporter_reference = CkJsonObject_stringOf(jResp,"exporter_reference"); importer_reference = CkJsonObject_stringOf(jResp,"importer_reference"); invoice = CkJsonObject_stringOf(jResp,"invoice"); commercial_invoice = CkJsonObject_BoolOf(jResp,"commercial_invoice"); license = CkJsonObject_stringOf(jResp,"license"); certificate = CkJsonObject_stringOf(jResp,"certificate"); notes = CkJsonObject_stringOf(jResp,"notes"); eel_pfc = CkJsonObject_stringOf(jResp,"eel_pfc"); aes_itn = CkJsonObject_stringOf(jResp,"aes_itn"); disclaimer = CkJsonObject_stringOf(jResp,"disclaimer"); incoterm = CkJsonObject_stringOf(jResp,"incoterm"); metadata = CkJsonObject_stringOf(jResp,"metadata"); test = CkJsonObject_BoolOf(jResp,"test"); duties_payor = CkJsonObject_stringOf(jResp,"duties_payor"); i = 0; count_i = CkJsonObject_SizeOfArray(jResp,"items"); while (i < count_i) { CkJsonObject_putI(jResp,i); strVal = CkJsonObject_stringOf(jResp,"items[i]"); i = i + 1; } CkHttp_Dispose(http); CkJsonObject_Dispose(json); CkStringBuilder_Dispose(sbResponseBody); CkJsonObject_Dispose(jResp); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.