Sample code for 30+ languages & platforms
Xbase++

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 Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oSocket
LOCAL nBSsl
LOCAL oRest
LOCAL nBAutoReconnect
LOCAL cResponseText

nSuccess := 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.

oSocket := CreateObject("Chilkat.Socket")
nBSsl := 1
nSuccess := oSocket:Connect("example.com", 443, nBSsl, 5000)
IF (nSuccess != 1)
    ? oSocket:LastErrorText
    oSocket:destroy()
    RETURN
ENDIF

oRest := CreateObject("Chilkat.Rest")

//  Use the connected socket as the transport.  The 2nd argument enables automatic reconnection.
nBAutoReconnect := 1
nSuccess := oRest:UseConnection(oSocket, nBAutoReconnect)
IF (nSuccess == 0)
    ? oRest:LastErrorText
    oSocket:destroy()
    oRest:destroy()
    RETURN
ENDIF

cResponseText := oRest:FullRequestNoBody("GET", "/api/status")
IF (oRest:LastMethodSuccess == 0)
    ? oRest:LastErrorText
    oSocket:destroy()
    oRest:destroy()
    RETURN
ENDIF

? cResponseText

oSocket:destroy()
oRest:destroy()