Sample code for 30+ languages & platforms
Unicode C

Send a REST Request using StringBuilder Bodies

See more REST Examples

Demonstrates Rest.FullRequestSb, which sends a complete request whose text body is read from a StringBuilder and stores the text response body in a second StringBuilder.

Background. Using StringBuilder objects for the request and response bodies avoids extra string conversions, which is helpful when working with large payloads.

Chilkat Unicode C Downloads

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

void ChilkatSample(void)
    {
    BOOL success;
    HCkRestW rest;
    BOOL bTls;
    BOOL bAutoReconnect;
    HCkStringBuilderW sbRequest;
    HCkStringBuilderW sbResponse;
    const wchar_t *responseText;

    success = FALSE;

    rest = CkRestW_Create();
    bTls = TRUE;
    bAutoReconnect = TRUE;
    success = CkRestW_Connect(rest,L"example.com",443,bTls,bAutoReconnect);
    if (success == FALSE) {
        wprintf(L"%s\n",CkRestW_lastErrorText(rest));
        CkRestW_Dispose(rest);
        return;
    }

    //  FullRequestSb sends a request whose text body is read from a StringBuilder and stores the text
    //  response body in a second StringBuilder.  This avoids extra string conversions for large bodies.
    sbRequest = CkStringBuilderW_Create();
    CkStringBuilderW_Append(sbRequest,L"{ \"query\": \"chilkat\" }");

    CkRestW_AddHeader(rest,L"Content-Type",L"application/json");

    sbResponse = CkStringBuilderW_Create();
    success = CkRestW_FullRequestSb(rest,L"POST",L"/api/search",sbRequest,sbResponse);
    if (success == FALSE) {
        wprintf(L"%s\n",CkRestW_lastErrorText(rest));
        CkRestW_Dispose(rest);
        CkStringBuilderW_Dispose(sbRequest);
        CkStringBuilderW_Dispose(sbResponse);
        return;
    }

    responseText = CkStringBuilderW_getAsString(sbResponse);
    if (CkStringBuilderW_getLastMethodSuccess(sbResponse) == FALSE) {
        wprintf(L"%s\n",CkStringBuilderW_lastErrorText(sbResponse));
        CkRestW_Dispose(rest);
        CkStringBuilderW_Dispose(sbRequest);
        CkStringBuilderW_Dispose(sbResponse);
        return;
    }

    wprintf(L"%s\n",responseText);


    CkRestW_Dispose(rest);
    CkStringBuilderW_Dispose(sbRequest);
    CkStringBuilderW_Dispose(sbResponse);

    }