Unicode C
Unicode C
REST Send multipart/form-data
See more REST Examples
Demonstrates how to send a multipart/form-data HTTP request.Chilkat Unicode C Downloads
#include <C_CkRestW.h>
#include <C_CkStringBuilderW.h>
#include <C_CkBinDataW.h>
void ChilkatSample(void)
{
BOOL success;
HCkRestW rest;
BOOL bTls;
int port;
BOOL bAutoReconnect;
HCkStringBuilderW sbHtml;
HCkBinDataW jpgBytes;
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.
// It will be different than what is shown here.
// The boundary string is also generated by Chilkat and will be different
// than shown below.
// 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");
jpgBytes = CkBinDataW_Create();
CkBinDataW_LoadFile(jpgBytes,L"qa_data/jpg/starfish.jpg");
CkRestW_SetMultipartBodyBd(rest,jpgBytes);
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);
CkBinDataW_Dispose(jpgBytes);
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);
CkBinDataW_Dispose(jpgBytes);
}