Sample code for 30+ languages & platforms
SQL Server

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 SQL Server Downloads

SQL Server
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 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.

    DECLARE @socket int
    EXEC @hr = sp_OACreate 'Chilkat.Socket', @socket OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    DECLARE @bSsl int
    SELECT @bSsl = 1
    EXEC sp_OAMethod @socket, 'Connect', @success OUT, 'example.com', 443, @bSsl, 5000
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @socket, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @socket
        RETURN
      END

    DECLARE @rest int
    EXEC @hr = sp_OACreate 'Chilkat.Rest', @rest OUT

    --  Use the connected socket as the transport.  The 2nd argument enables automatic reconnection.
    DECLARE @bAutoReconnect int
    SELECT @bAutoReconnect = 1
    EXEC sp_OAMethod @rest, 'UseConnection', @success OUT, @socket, @bAutoReconnect
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @socket
        EXEC @hr = sp_OADestroy @rest
        RETURN
      END

    DECLARE @responseText nvarchar(4000)
    EXEC sp_OAMethod @rest, 'FullRequestNoBody', @responseText OUT, 'GET', '/api/status'
    EXEC sp_OAGetProperty @rest, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 = 0
      BEGIN
        EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @socket
        EXEC @hr = sp_OADestroy @rest
        RETURN
      END

    PRINT @responseText

    EXEC @hr = sp_OADestroy @socket
    EXEC @hr = sp_OADestroy @rest


END
GO