Chilkat • HOME • Android™ • AutoIt • C • C# • C++ • Chilkat2-Python • CkPython • Classic ASP • DataFlex • Delphi DLL • Go • Java • Node.js • Objective-C • PHP Extension • Perl • PowerBuilder • PowerShell • PureBasic • Ruby • SQL Server • Swift • Tcl • Unicode C • Unicode C++ • VB.NET • VBScript • Visual Basic 6.0 • Visual FoxPro • Xojo Plugin
(Unicode C) REST POST JSON using Gzip Content EncodingDemonstrates how to send a JSON POST using the gzip Content-Encoding.
#include <C_CkJsonObjectW.h> #include <C_CkRestW.h> #include <C_CkStringBuilderW.h> void ChilkatSample(void) { HCkJsonObjectW json; HCkRestW rest; BOOL success; HCkStringBuilderW sbReq; HCkStringBuilderW sbResp; HCkJsonObjectW jsonResp; // This requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. // Use the online tool at https://tools.chilkat.io/Default.cshtml // to generate the JSON code that creates this JSON: // { // "lists": [ // { // "id": "1511199999" // } // ], // "confirmed": false, // "email_addresses": [ // { // "email_address": "support@chilkatsoft.com" // } // ], // "first_name": "Matt", // "last_name": "Smith" // } // json = CkJsonObjectW_Create(); CkJsonObjectW_UpdateString(json,L"lists[0].id",L"1511199999"); CkJsonObjectW_UpdateBool(json,L"confirmed",FALSE); CkJsonObjectW_UpdateString(json,L"email_addresses[0].email_address",L"support@chilkatsoft.com"); CkJsonObjectW_UpdateString(json,L"first_name",L"Matt"); CkJsonObjectW_UpdateString(json,L"last_name",L"Smith"); rest = CkRestW_Create(); success = CkRestW_Connect(rest,L"api.constantcontact.com",443,TRUE,TRUE); if (success != TRUE) { wprintf(L"%s\n",CkRestW_lastErrorText(rest)); CkJsonObjectW_Dispose(json); CkRestW_Dispose(rest); return; } CkRestW_AddHeader(rest,L"Content-Type",L"application/json"); CkRestW_AddHeader(rest,L"Authorization",L"Bearer xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx"); // Tell the server you'll accept only an application/json response. CkRestW_AddHeader(rest,L"Accept",L"application/json"); // Indicate that we'll be sending the request body gzipped. CkRestW_AddHeader(rest,L"Content-Encoding",L"gzip"); sbReq = CkStringBuilderW_Create(); CkJsonObjectW_EmitSb(json,sbReq); // Send the POST. sbResp = CkStringBuilderW_Create(); success = CkRestW_FullRequestSb(rest,L"POST",L"/v2/contacts?action_by=ACTION_BY_VISITOR&api_key=xxxxxxx",sbReq,sbResp); if (success != TRUE) { wprintf(L"%s\n",CkRestW_lastErrorText(rest)); CkJsonObjectW_Dispose(json); CkRestW_Dispose(rest); CkStringBuilderW_Dispose(sbReq); CkStringBuilderW_Dispose(sbResp); return; } wprintf(L"Response body:\n"); wprintf(L"%s\n",CkStringBuilderW_getAsString(sbResp)); if (CkRestW_getResponseStatusCode(rest) != 200) { wprintf(L"Received error response code: %d\n",CkRestW_getResponseStatusCode(rest)); CkJsonObjectW_Dispose(json); CkRestW_Dispose(rest); CkStringBuilderW_Dispose(sbReq); CkStringBuilderW_Dispose(sbResp); return; } jsonResp = CkJsonObjectW_Create(); CkJsonObjectW_LoadSb(jsonResp,sbResp); // Use the online tool at https://tools.chilkat.io/jsonParse.cshtml // to generate the JSON code that parses the JSON response.. CkJsonObjectW_Dispose(json); CkRestW_Dispose(rest); CkStringBuilderW_Dispose(sbReq); CkStringBuilderW_Dispose(sbResp); CkJsonObjectW_Dispose(jsonResp); } |
© 2000-2025 Chilkat Software, Inc. All Rights Reserved.