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

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

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

    //  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 = CkBinData_Create();
    bLoaded = CkBinData_LoadFile(bdRequest,"qa_data/image.png");
    if (bLoaded != TRUE) {
        printf("Failed to load the request body file.\n");
        CkRest_Dispose(rest);
        CkBinData_Dispose(bdRequest);
        return;
    }

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

    sbResponse = CkStringBuilder_Create();
    success = CkRest_FullRequestBd(rest,"PUT","/api/images/1",bdRequest,sbResponse);
    if (success == FALSE) {
        printf("%s\n",CkRest_lastErrorText(rest));
        CkRest_Dispose(rest);
        CkBinData_Dispose(bdRequest);
        CkStringBuilder_Dispose(sbResponse);
        return;
    }

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

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


    CkRest_Dispose(rest);
    CkBinData_Dispose(bdRequest);
    CkStringBuilder_Dispose(sbResponse);

    }