Sample code for 30+ languages & platforms
C

Read the REST Response Body into a Stream

See more REST Examples

Demonstrates Rest.ReadRespBodyStream, which reads the response body into a Stream. The optional flag sets the stream character set from the response Content-Type charset for textual responses.

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. The staged request model separates sending from reading: a SendReq method transmits the request, ReadResponseHeader reads the status line and headers, and a ReadResp method reads the body. This gives fine control over large or streamed messages compared with the single-call full-request methods.

Chilkat C Downloads

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

void ChilkatSample(void)
    {
    BOOL success;
    HCkRest rest;
    BOOL bTls;
    BOOL bAutoReconnect;
    int statusCode;
    HCkStream respStream;
    BOOL bAutoSetCharset;

    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.

    success = CkRest_SendReqNoBody(rest,"GET","/api/images/1");
    if (success == FALSE) {
        printf("%s\n",CkRest_lastErrorText(rest));
        CkRest_Dispose(rest);
        return;
    }

    statusCode = CkRest_ReadResponseHeader(rest);
    if (statusCode < 0) {
        printf("%s\n",CkRest_lastErrorText(rest));
        CkRest_Dispose(rest);
        return;
    }

    //  Read the response body into a Stream.  Here the stream's sink is a local file, which is
    //  appropriate for large or binary responses.
    respStream = CkStream_Create();
    CkStream_putSinkFile(respStream,"qa_output/image.png");

    //  If the 2nd argument is TRUE and the response is textual, the stream's character set is set from
    //  the response Content-Type charset.  It has no effect for binary responses.
    bAutoSetCharset = TRUE;
    success = CkRest_ReadRespBodyStream(rest,respStream,bAutoSetCharset);
    if (success == FALSE) {
        printf("%s\n",CkRest_lastErrorText(rest));
        CkRest_Dispose(rest);
        CkStream_Dispose(respStream);
        return;
    }

    printf("Response body written to the stream sink.\n");


    CkRest_Dispose(rest);
    CkStream_Dispose(respStream);

    }