Sample code for 30+ languages & platforms
Unicode C

Set a Multipart Part Body from BinData

See more REST Examples

Demonstrates Rest.SetMultipartBodyBd, which sets the binary body of the selected multipart part from a BinData.

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. A multipart request is assembled part by part: the part selector chooses the part being built, header fields describe it, and a SetMultipartBody method supplies its content. The request is then sent with a multipart send or full-request method.

Chilkat Unicode C Downloads

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

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

    //  Select the multipart part to build, and set its header fields.
    CkRestW_putPartSelector(rest,L"1");
    CkRestW_AddHeader(rest,L"Content-Disposition",L"form-data; name=\"field1\"");
    CkRestW_AddHeader(rest,L"Content-Type",L"image/png");

    //  Set the binary body of the selected part from a BinData.
    bdBody = CkBinDataW_Create();
    bLoaded = CkBinDataW_LoadFile(bdBody,L"qa_data/image.png");
    if (bLoaded != TRUE) {
        wprintf(L"Failed to load the part body file.\n");
        CkRestW_Dispose(rest);
        CkBinDataW_Dispose(bdBody);
        return;
    }

    success = CkRestW_SetMultipartBodyBd(rest,bdBody);
    if (success == FALSE) {
        wprintf(L"%s\n",CkRestW_lastErrorText(rest));
        CkRestW_Dispose(rest);
        CkBinDataW_Dispose(bdBody);
        return;
    }

    responseText = CkRestW_fullRequestMultipart(rest,L"POST",L"/api/upload");
    if (CkRestW_getLastMethodSuccess(rest) == FALSE) {
        wprintf(L"%s\n",CkRestW_lastErrorText(rest));
        CkRestW_Dispose(rest);
        CkBinDataW_Dispose(bdBody);
        return;
    }

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


    CkRestW_Dispose(rest);
    CkBinDataW_Dispose(bdBody);

    }