Sample code for 30+ languages & platforms
SQL Server

Explaining the Rest ClearResponseBodyStream Method

See more Azure Cloud Storage Examples

The ClearResponseBodyStream method would be needed if your applicaiton called SetResponseBodyStream to send the response to a stream for one request, but then wants to subsequently do something else. The application must call ClearResponseBodyStream to no longer send responses to the stream.

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
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    -- This example requires the Chilkat API to have been previously unlocked.
    -- See Global Unlock Sample for sample code.

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

    -- Connect to the web server
    DECLARE @bTls int
    SELECT @bTls = 1
    DECLARE @port int
    SELECT @port = 443
    DECLARE @bAutoReconnect int
    SELECT @bAutoReconnect = 1
    EXEC sp_OAMethod @rest, 'Connect', @success OUT, 'www.chilkatsoft.com', @port, @bTls, @bAutoReconnect
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @rest
        RETURN
      END

    -- Setup a file stream for the download
    DECLARE @fileStream int
    EXEC @hr = sp_OACreate 'Chilkat.Stream', @fileStream OUT

    EXEC sp_OASetProperty @fileStream, 'SinkFile', 'qa_output/starfish.jpg'

    -- Indicate that the call to FullRequestNoBody should send the response body
    -- to fileStream if the response status code is 200.
    -- If a non-success response status code is received, then nothing
    -- is streamed to the output file and the error response is returned by FullRequestNoBody.
    DECLARE @expectedStatus int
    SELECT @expectedStatus = 200
    EXEC sp_OAMethod @rest, 'SetResponseBodyStream', @success OUT, @expectedStatus, 1, @fileStream

    DECLARE @responseStr nvarchar(4000)
    EXEC sp_OAMethod @rest, 'FullRequestNoBody', @responseStr OUT, 'GET', '/images/starfish.jpg'
    EXEC sp_OAGetProperty @rest, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 = 0
      BEGIN
        -- Examine the request/response to see what happened.

        EXEC sp_OAGetProperty @rest, 'ResponseStatusCode', @iTmp0 OUT
        PRINT 'response status code = ' + @iTmp0

        EXEC sp_OAGetProperty @rest, 'ResponseStatusText', @sTmp0 OUT
        PRINT 'response status text = ' + @sTmp0

        EXEC sp_OAGetProperty @rest, 'ResponseHeader', @sTmp0 OUT
        PRINT 'response header: ' + @sTmp0

        PRINT 'response body (if any): ' + @responseStr

        PRINT '---'

        EXEC sp_OAGetProperty @rest, 'LastRequestStartLine', @sTmp0 OUT
        PRINT 'LastRequestStartLine: ' + @sTmp0

        EXEC sp_OAGetProperty @rest, 'LastRequestHeader', @sTmp0 OUT
        PRINT 'LastRequestHeader: ' + @sTmp0
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @fileStream
        RETURN
      END


    PRINT 'downloaded starfish.jpg'

    -- Clear the response body stream previously set in the call to SetResponseBodyStream.
    EXEC sp_OAMethod @rest, 'ClearResponseBodyStream', NULL

    -- Download something else, but this time send the response body to bd.
    DECLARE @bd int
    EXEC @hr = sp_OACreate 'Chilkat.BinData', @bd OUT

    EXEC sp_OAMethod @rest, 'FullRequestNoBodyBd', @success OUT, 'GET', '/images/penguins.jpg', @bd
    IF @success = 0
      BEGIN
        -- Examine the request/response to see what happened.

        EXEC sp_OAGetProperty @rest, 'ResponseStatusCode', @iTmp0 OUT
        PRINT 'response status code = ' + @iTmp0

        EXEC sp_OAGetProperty @rest, 'ResponseStatusText', @sTmp0 OUT
        PRINT 'response status text = ' + @sTmp0

        EXEC sp_OAGetProperty @rest, 'ResponseHeader', @sTmp0 OUT
        PRINT 'response header: ' + @sTmp0

        EXEC sp_OAMethod @bd, 'GetString', @sTmp0 OUT, 'utf-8'
        PRINT 'response body (if any): ' + @sTmp0

        PRINT '---'

        EXEC sp_OAGetProperty @rest, 'LastRequestStartLine', @sTmp0 OUT
        PRINT 'LastRequestStartLine: ' + @sTmp0

        EXEC sp_OAGetProperty @rest, 'LastRequestHeader', @sTmp0 OUT
        PRINT 'LastRequestHeader: ' + @sTmp0
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @fileStream
        EXEC @hr = sp_OADestroy @bd
        RETURN
      END

    -- Save the contents of bd to a file.
    EXEC sp_OAMethod @bd, 'WriteFile', @success OUT, 'qa_output/penguins.jpg'


    PRINT 'Success: ' + @success

    EXEC @hr = sp_OADestroy @rest
    EXEC @hr = sp_OADestroy @fileStream
    EXEC @hr = sp_OADestroy @bd


END
GO