C
C
Set a Multipart Part Body from a Stream
See more REST Examples
Demonstrates Rest.SetMultipartBodyStream, which uses a Stream as the body source for the selected multipart part.
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. Streaming a part body is appropriate for large parts because the data is read incrementally from the source rather than being buffered entirely in memory. The part is otherwise assembled the same way — select the part, set its headers, then set its body.
Chilkat C Downloads
#include <C_CkRest.h>
#include <C_CkStream.h>
void ChilkatSample(void)
{
BOOL success;
HCkRest rest;
BOOL bTls;
BOOL bAutoReconnect;
HCkStream partStream;
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.
// Select the multipart part to build, and set its header fields.
CkRest_putPartSelector(rest,"1");
CkRest_AddHeader(rest,"Content-Disposition","form-data; name=\"field1\"");
CkRest_AddHeader(rest,"Content-Type","application/octet-stream");
// Use a Stream as the body source for the selected part. The stream's source is a local file,
// which is appropriate for large parts.
partStream = CkStream_Create();
CkStream_putSourceFile(partStream,"qa_data/attachment.bin");
success = CkRest_SetMultipartBodyStream(rest,partStream);
if (success == FALSE) {
printf("%s\n",CkRest_lastErrorText(rest));
CkRest_Dispose(rest);
CkStream_Dispose(partStream);
return;
}
responseText = CkRest_fullRequestMultipart(rest,"POST","/api/upload");
if (CkRest_getLastMethodSuccess(rest) == FALSE) {
printf("%s\n",CkRest_lastErrorText(rest));
CkRest_Dispose(rest);
CkStream_Dispose(partStream);
return;
}
printf("%s\n",responseText);
CkRest_Dispose(rest);
CkStream_Dispose(partStream);
}