Sample code for 30+ languages & platforms
PureBasic

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 PureBasic Downloads

PureBasic
IncludeFile "CkRest.pb"
IncludeFile "CkSocket.pb"

Procedure ChilkatExample()

    success.i = 0

    ;  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.i = CkSocket::ckCreate()
    If socket.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    bSsl.i = 1
    success = CkSocket::ckConnect(socket,"example.com",443,bSsl,5000)
    If success <> 1
        Debug CkSocket::ckLastErrorText(socket)
        CkSocket::ckDispose(socket)
        ProcedureReturn
    EndIf

    rest.i = CkRest::ckCreate()
    If rest.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ;  Use the connected socket as the transport.  The 2nd argument enables automatic reconnection.
    bAutoReconnect.i = 1
    success = CkRest::ckUseConnection(rest,socket,bAutoReconnect)
    If success = 0
        Debug CkRest::ckLastErrorText(rest)
        CkSocket::ckDispose(socket)
        CkRest::ckDispose(rest)
        ProcedureReturn
    EndIf

    responseText.s = CkRest::ckFullRequestNoBody(rest,"GET","/api/status")
    If CkRest::ckLastMethodSuccess(rest) = 0
        Debug CkRest::ckLastErrorText(rest)
        CkSocket::ckDispose(socket)
        CkRest::ckDispose(rest)
        ProcedureReturn
    EndIf

    Debug responseText


    CkSocket::ckDispose(socket)
    CkRest::ckDispose(rest)


    ProcedureReturn
EndProcedure