Sample code for 30+ languages & platforms
C

Use an Existing Socket Connection for REST

See more REST Examples

Demonstrates Rest.UseConnection, which supplies an already-connected Socket as the transport for REST requests instead of calling Connect. This is useful when the connection must be established with specific socket options or shared across objects.

Background. Rest can operate over any connected socket. The second argument enables automatic reconnection so a dropped connection is transparently re-established between requests.

Chilkat C Downloads

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

void ChilkatSample(void)
    {
    BOOL success;
    HCkSocket socket;
    BOOL bSsl;
    HCkRest rest;
    BOOL bAutoReconnect;
    const char *responseText;

    success = FALSE;

    //  Demonstrates supplying an already-connected socket for the REST transport.  This is useful when
    //  the connection must be established with specific socket options, or reused across objects.

    socket = CkSocket_Create();
    bSsl = TRUE;
    success = CkSocket_Connect(socket,"example.com",443,bSsl,5000);
    if (success != TRUE) {
        printf("%s\n",CkSocket_lastErrorText(socket));
        CkSocket_Dispose(socket);
        return;
    }

    rest = CkRest_Create();

    //  Use the connected socket as the transport.  The 2nd argument enables automatic reconnection.
    bAutoReconnect = TRUE;
    success = CkRest_UseConnection(rest,socket,bAutoReconnect);
    if (success == FALSE) {
        printf("%s\n",CkRest_lastErrorText(rest));
        CkSocket_Dispose(socket);
        CkRest_Dispose(rest);
        return;
    }

    responseText = CkRest_fullRequestNoBody(rest,"GET","/api/status");
    if (CkRest_getLastMethodSuccess(rest) == FALSE) {
        printf("%s\n",CkRest_lastErrorText(rest));
        CkSocket_Dispose(socket);
        CkRest_Dispose(rest);
        return;
    }

    printf("%s\n",responseText);


    CkSocket_Dispose(socket);
    CkRest_Dispose(rest);

    }