Sample code for 30+ languages & platforms
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 C Downloads

C
#include <C_CkRest.h>
#include <C_CkBinData.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkRest rest;
    BOOL bTls;
    BOOL bAutoReconnect;
    HCkBinData bdResponse;
    BOOL bSaved;

    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.

    //  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 = CkBinData_Create();
    success = CkRest_FullRequestNoBodyBd(rest,"GET","/api/images/1",bdResponse);
    if (success == FALSE) {
        printf("%s\n",CkRest_lastErrorText(rest));
        CkRest_Dispose(rest);
        CkBinData_Dispose(bdResponse);
        return;
    }

    bSaved = CkBinData_WriteFile(bdResponse,"qa_output/image.png");
    if (bSaved != TRUE) {
        printf("Failed to save the response body.\n");
        CkRest_Dispose(rest);
        CkBinData_Dispose(bdResponse);
        return;
    }

    printf("Downloaded %d bytes.\n",CkBinData_getNumBytes(bdResponse));


    CkRest_Dispose(rest);
    CkBinData_Dispose(bdResponse);

    }