Sample code for 30+ languages & platforms
Unicode C

Set a Multipart Part Body from a StringBuilder

See more REST Examples

Demonstrates Rest.SetMultipartBodySb, which sets the text body of the selected multipart part from a StringBuilder.

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_CkStringBuilderW.h>

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

    //  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"application/json");

    //  Set the text body of the selected part from a StringBuilder.
    sbBody = CkStringBuilderW_Create();
    CkStringBuilderW_Append(sbBody,L"{ \"key\": \"value\" }");
    success = CkRestW_SetMultipartBodySb(rest,sbBody);
    if (success == FALSE) {
        wprintf(L"%s\n",CkRestW_lastErrorText(rest));
        CkRestW_Dispose(rest);
        CkStringBuilderW_Dispose(sbBody);
        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);
        CkStringBuilderW_Dispose(sbBody);
        return;
    }

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


    CkRestW_Dispose(rest);
    CkStringBuilderW_Dispose(sbBody);

    }