Sample code for 30+ languages & platforms
Unicode C

Download a Binary REST Response into BinData

See more REST Examples

Demonstrates Rest.FullRequestNoBodyBd, which sends a request with no body and stores the binary response body in 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. This method is appropriate when the response is binary, such as an image or file download, allowing the received bytes to be saved or processed directly.

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 bdResponse;
    BOOL bSaved;

    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.

    //  FullRequestNoBodyBd sends a request with no body and stores the binary response body in a
    //  BinData.  This is appropriate when the response is binary, such as an image or file download.
    bdResponse = CkBinDataW_Create();
    success = CkRestW_FullRequestNoBodyBd(rest,L"GET",L"/api/images/1",bdResponse);
    if (success == FALSE) {
        wprintf(L"%s\n",CkRestW_lastErrorText(rest));
        CkRestW_Dispose(rest);
        CkBinDataW_Dispose(bdResponse);
        return;
    }

    bSaved = CkBinDataW_WriteFile(bdResponse,L"qa_output/image.png");
    if (bSaved != TRUE) {
        wprintf(L"Failed to save the response body.\n");
        CkRestW_Dispose(rest);
        CkBinDataW_Dispose(bdResponse);
        return;
    }

    wprintf(L"Downloaded %d bytes.\n",CkBinDataW_getNumBytes(bdResponse));


    CkRestW_Dispose(rest);
    CkBinDataW_Dispose(bdResponse);

    }