Sample code for 30+ languages & platforms
SQL Server

Read the REST Response Body into BinData

See more REST Examples

Demonstrates Rest.ReadRespBd, which reads the response body as raw bytes into a BinData, replacing its previous contents.

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 @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.

    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 as raw bytes into a BinData.
    DECLARE @bdResponse int
    EXEC @hr = sp_OACreate 'Chilkat.BinData', @bdResponse OUT

    EXEC sp_OAMethod @rest, 'ReadRespBd', @success OUT, @bdResponse
    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 @bdResponse
        RETURN
      END

    DECLARE @bSaved int
    EXEC sp_OAMethod @bdResponse, 'WriteFile', @bSaved OUT, 'qa_output/image.png'
    IF @bSaved <> 1
      BEGIN

        PRINT 'Failed to save the response body.'
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @sbError
        EXEC @hr = sp_OADestroy @bdResponse
        RETURN
      END

    EXEC sp_OAGetProperty @bdResponse, 'NumBytes', @iTmp0 OUT

    PRINT 'Received ' + @iTmp0 + ' bytes.'

    EXEC @hr = sp_OADestroy @rest
    EXEC @hr = sp_OADestroy @sbError
    EXEC @hr = sp_OADestroy @bdResponse


END
GO