Sample code for 30+ languages & platforms
Unicode C

REST Stream Multipart Body from File

See more REST Examples

Demonstrates how to send a multipart/form-data HTTP request, where one of the parts contains data streamed directly from a file. This is good for cases where the file to be uploaded is very large.

Chilkat Unicode C Downloads

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

void ChilkatSample(void)
    {
    BOOL success;
    HCkRestW rest;
    BOOL bTls;
    int port;
    BOOL bAutoReconnect;
    HCkStringBuilderW sbHtml;
    HCkStreamW fileStream;
    const wchar_t *responseBody;

    success = FALSE;

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    rest = CkRestW_Create();

    // Connect to the destination web server.
    bTls = TRUE;
    port = 443;
    bAutoReconnect = TRUE;
    success = CkRestW_Connect(rest,L"www.somewebserver.com",port,bTls,bAutoReconnect);

    // This example will send the following multipart/form-data request.
    // The Content-Length is automatically computed and added by Chilkat.

    // 	POST /some_path HTTP/1.1
    // 	Content-Type: multipart/form-data; boundary=---------------------------735323031399963166993862150
    // 	Content-Length: 834
    // 
    // 	-----------------------------735323031399963166993862150
    // 	Content-Disposition: form-data; name="text1"
    // 
    // 	text 123 abc
    // 	-----------------------------735323031399963166993862150
    // 	Content-Disposition: form-data; name="text2"
    // 
    // 	xyz
    // 	-----------------------------735323031399963166993862150
    // 	Content-Disposition: form-data; name="file1"; filename="a.txt"
    // 	Content-Type: text/plain
    // 
    // 	Content of a.txt.
    // 
    // 	-----------------------------735323031399963166993862150
    // 	Content-Disposition: form-data; name="file2"; filename="a.html"
    // 	Content-Type: text/html
    // 
    // 	<!DOCTYPE html><title>Content of a.html.</title>
    // 
    // 	-----------------------------735323031399963166993862150
    // 	Content-Disposition: form-data; name="file3"; filename="starfish.jpg"
    // 	Content-Type: image/jpeg
    // 
    // 	binary data goes here
    // 	-----------------------------735323031399963166993862150--

    // Set the Content-Type for the topmost MIME part.
    CkRestW_AddHeader(rest,L"Content-Type",L"multipart/form-data");

    // Specify each part of the request.
    CkRestW_putPartSelector(rest,L"1");
    CkRestW_AddHeader(rest,L"Content-Disposition",L"form-data; name=\"text1\"");
    CkRestW_SetMultipartBodyString(rest,L"text 123 abc");

    CkRestW_putPartSelector(rest,L"2");
    CkRestW_AddHeader(rest,L"Content-Disposition",L"form-data; name=\"text2\"");
    CkRestW_SetMultipartBodyString(rest,L"xyz");

    CkRestW_putPartSelector(rest,L"3");
    CkRestW_AddHeader(rest,L"Content-Disposition",L"form-data; name=\"file1\"; filename=\"a.txt\"");
    CkRestW_AddHeader(rest,L"Content-Type",L"text/plain");
    CkRestW_SetMultipartBodyString(rest,L"Content of a.txt.");

    CkRestW_putPartSelector(rest,L"4");
    CkRestW_AddHeader(rest,L"Content-Disposition",L"form-data; name=\"file2\"; filename=\"a.html\"");
    CkRestW_AddHeader(rest,L"Content-Type",L"text/html");
    sbHtml = CkStringBuilderW_Create();
    CkStringBuilderW_LoadFile(sbHtml,L"qa_data/html/a.html",L"utf-8");
    CkRestW_SetMultipartBodySb(rest,sbHtml);

    CkRestW_putPartSelector(rest,L"5");
    CkRestW_AddHeader(rest,L"Content-Disposition",L"form-data; name=\"file3\"; filename=\"starfish.jpg\"");
    CkRestW_AddHeader(rest,L"Content-Type",L"image/jpeg");

    // When the request is sent, stream this part directly from the file.
    // This avoids having to load the entire file into memory.
    fileStream = CkStreamW_Create();
    CkStreamW_putSourceFile(fileStream,L"qa_data/jpg/starfish.jpg");
    CkRestW_SetMultipartBodyStream(rest,fileStream);

    responseBody = CkRestW_fullRequestMultipart(rest,L"POST",L"/some_path");
    if (CkRestW_getLastMethodSuccess(rest) != TRUE) {
        wprintf(L"%s\n",CkRestW_lastErrorText(rest));
        CkRestW_Dispose(rest);
        CkStringBuilderW_Dispose(sbHtml);
        CkStreamW_Dispose(fileStream);
        return;
    }

    // ...
    // ...

    // Clear the REST object for any subsequent requests..
    CkRestW_ClearAllHeaders(rest);
    CkRestW_ClearAllParts(rest);
    CkRestW_putPartSelector(rest,L"");


    CkRestW_Dispose(rest);
    CkStringBuilderW_Dispose(sbHtml);
    CkStreamW_Dispose(fileStream);

    }