Sample code for 30+ languages & platforms
Unicode C

REST POST JSON using Gzip Content Encoding

See more REST Examples

Demonstrates how to send a JSON POST using the gzip Content-Encoding.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkJsonObjectW.h>
#include <C_CkRestW.h>
#include <C_CkStringBuilderW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkJsonObjectW json;
    HCkRestW rest;
    HCkStringBuilderW sbReq;
    HCkStringBuilderW sbResp;
    HCkJsonObjectW jsonResp;

    success = FALSE;

    // 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);

    }