Sample code for 30+ languages & platforms
Unicode C

Send a REST Request with a Binary Body

See more REST Examples

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

The file paths are relative to the application's current working directory. Absolute paths may also be used. Supply the paths appropriate to your own environment.

Background. FullRequestBd is used when the request payload is binary, such as an image or other file, while the response is textual (for example a JSON acknowledgement).

Chilkat Unicode C Downloads

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

void ChilkatSample(void)
    {
    BOOL success;
    HCkRestW rest;
    BOOL bTls;
    BOOL bAutoReconnect;
    HCkBinDataW bdRequest;
    BOOL bLoaded;
    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;
    }

    //  The file paths are relative to the application's current working directory.  Absolute paths
    //  may also be used.  Supply the paths appropriate to your own environment.

    //  FullRequestBd sends a request whose binary body is read from a BinData and stores the text
    //  response body in a StringBuilder.
    bdRequest = CkBinDataW_Create();
    bLoaded = CkBinDataW_LoadFile(bdRequest,L"qa_data/image.png");
    if (bLoaded != TRUE) {
        wprintf(L"Failed to load the request body file.\n");
        CkRestW_Dispose(rest);
        CkBinDataW_Dispose(bdRequest);
        return;
    }

    CkRestW_AddHeader(rest,L"Content-Type",L"image/png");

    sbResponse = CkStringBuilderW_Create();
    success = CkRestW_FullRequestBd(rest,L"PUT",L"/api/images/1",bdRequest,sbResponse);
    if (success == FALSE) {
        wprintf(L"%s\n",CkRestW_lastErrorText(rest));
        CkRestW_Dispose(rest);
        CkBinDataW_Dispose(bdRequest);
        CkStringBuilderW_Dispose(sbResponse);
        return;
    }

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

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


    CkRestW_Dispose(rest);
    CkBinDataW_Dispose(bdRequest);
    CkStringBuilderW_Dispose(sbResponse);

    }