Sample code for 30+ languages & platforms
SQL Server

Stream a REST Response Body to a Destination

See more REST Examples

Demonstrates Rest.SetResponseBodyStream, which configures a Stream destination for response bodies received by the full-request convenience methods. The body is written to the stream only when the response status code matches the expected status.

The file paths are relative to the application's current working directory. Absolute paths may also be used. Supply the paths appropriate to your own environment.

Background. Directing the response body to a stream avoids holding a large response in memory. The expected-status argument ensures error responses (which typically have small bodies) are not written to the stream destination.

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

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

    DECLARE @bTls int
    SELECT @bTls = 1
    DECLARE @bAutoReconnect int
    SELECT @bAutoReconnect = 1
    EXEC sp_OAMethod @rest, 'Connect', @success OUT, 'example.com', 443, @bTls, @bAutoReconnect
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @rest
        RETURN
      END
    --  The file paths are relative to the application's current working directory.  Absolute paths
    --  may also be used.  Supply the paths appropriate to your own environment.

    --  Configure a Stream destination for response bodies received by the full-request convenience
    --  methods.  The body is written to the stream only when the response status code matches the 1st
    --  argument.
    DECLARE @respStream int
    EXEC @hr = sp_OACreate 'Chilkat.Stream', @respStream OUT

    EXEC sp_OASetProperty @respStream, 'SinkFile', 'qa_output/response_body.dat'

    --  The 2nd argument sets the stream character set from the response Content-Type charset for textual
    --  responses.  The 3rd argument is the destination Stream.
    DECLARE @bAutoSetCharset int
    SELECT @bAutoSetCharset = 1
    DECLARE @expectedStatus int
    SELECT @expectedStatus = 200
    EXEC sp_OAMethod @rest, 'SetResponseBodyStream', @success OUT, @expectedStatus, @bAutoSetCharset, @respStream
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @respStream
        RETURN
      END

    --  The full-request call now streams the response body to the sink instead of returning it.
    DECLARE @responseText nvarchar(4000)
    EXEC sp_OAMethod @rest, 'FullRequestNoBody', @responseText OUT, 'GET', '/api/largefile'
    EXEC sp_OAGetProperty @rest, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 = 0
      BEGIN
        EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @respStream
        RETURN
      END

    EXEC sp_OAGetProperty @rest, 'ResponseStatusCode', @iTmp0 OUT
    PRINT 'Response streamed to file.  Status: ' + @iTmp0

    EXEC @hr = sp_OADestroy @rest
    EXEC @hr = sp_OADestroy @respStream


END
GO