Sample code for 30+ languages & platforms
SQL Server

Read the REST Response Body into a Stream

See more REST Examples

Demonstrates Rest.ReadRespBodyStream, which reads the response body into a Stream. The optional flag sets the stream character set from the response Content-Type charset for textual responses.

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. The staged request model separates sending from reading: a SendReq method transmits the request, ReadResponseHeader reads the status line and headers, and a ReadResp method reads the body. This gives fine control over large or streamed messages compared with the single-call full-request methods.

Chilkat SQL Server Downloads

SQL Server
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr 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.

    EXEC sp_OAMethod @rest, 'SendReqNoBody', @success OUT, 'GET', '/api/images/1'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @rest
        RETURN
      END

    DECLARE @statusCode int
    EXEC sp_OAMethod @rest, 'ReadResponseHeader', @statusCode OUT
    IF @statusCode < 0
      BEGIN
        EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @rest
        RETURN
      END

    --  Check for the expected success status code.  If the response is not 200, the body is likely
    --  error text formatted as JSON, XML, or HTML rather than the expected binary content.  In that case
    --  read the body as text, report it, and return.
    IF @statusCode <> 200
      BEGIN
        DECLARE @sbError int
        EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbError OUT

        EXEC sp_OAMethod @rest, 'ReadRespSb', @success OUT, @sbError
        IF @success = 0
          BEGIN
            EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
            PRINT @sTmp0
            EXEC @hr = sp_OADestroy @rest
            EXEC @hr = sp_OADestroy @sbError
            RETURN
          END

        PRINT 'Request failed with status code ' + @statusCode
        EXEC sp_OAMethod @sbError, 'GetAsString', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @sbError
        RETURN
      END

    --  The status code is 200.  Read the response body into a Stream.  Here the stream's sink is a local
    --  file, which is appropriate for large or binary responses.
    DECLARE @respStream int
    EXEC @hr = sp_OACreate 'Chilkat.Stream', @respStream OUT

    EXEC sp_OASetProperty @respStream, 'SinkFile', 'qa_output/image.png'

    --  If the 2nd argument is 1 and the response is textual, the stream's character set is set from
    --  the response Content-Type charset.  It has no effect for binary responses.
    DECLARE @bAutoSetCharset int
    SELECT @bAutoSetCharset = 1
    EXEC sp_OAMethod @rest, 'ReadRespBodyStream', @success OUT, @respStream, @bAutoSetCharset
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @sbError
        EXEC @hr = sp_OADestroy @respStream
        RETURN
      END

    PRINT 'Response body written to the stream sink.'

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


END
GO