Sample code for 30+ languages & platforms
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 C Downloads

C
#include <C_CkRest.h>
#include <C_CkStringBuilder.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkRest rest;
    BOOL bTls;
    BOOL bAutoReconnect;
    HCkStringBuilder sbRequest;
    HCkStringBuilder sbResponse;
    const char *responseText;

    success = FALSE;

    rest = CkRest_Create();
    bTls = TRUE;
    bAutoReconnect = TRUE;
    success = CkRest_Connect(rest,"example.com",443,bTls,bAutoReconnect);
    if (success == FALSE) {
        printf("%s\n",CkRest_lastErrorText(rest));
        CkRest_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 = CkStringBuilder_Create();
    CkStringBuilder_Append(sbRequest,"{ \"query\": \"chilkat\" }");

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

    sbResponse = CkStringBuilder_Create();
    success = CkRest_FullRequestSb(rest,"POST","/api/search",sbRequest,sbResponse);
    if (success == FALSE) {
        printf("%s\n",CkRest_lastErrorText(rest));
        CkRest_Dispose(rest);
        CkStringBuilder_Dispose(sbRequest);
        CkStringBuilder_Dispose(sbResponse);
        return;
    }

    responseText = CkStringBuilder_getAsString(sbResponse);
    if (CkStringBuilder_getLastMethodSuccess(sbResponse) == FALSE) {
        printf("%s\n",CkStringBuilder_lastErrorText(sbResponse));
        CkRest_Dispose(rest);
        CkStringBuilder_Dispose(sbRequest);
        CkStringBuilder_Dispose(sbResponse);
        return;
    }

    printf("%s\n",responseText);


    CkRest_Dispose(rest);
    CkStringBuilder_Dispose(sbRequest);
    CkStringBuilder_Dispose(sbResponse);

    }