SQL Server
SQL Server
Download a Binary REST Response into BinData
See more REST Examples
Demonstrates Rest.FullRequestNoBodyBd, which sends a request with no body and stores the binary response body in a BinData.
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. This method is appropriate when the response is binary, such as an image or file download, allowing the received bytes to be saved or processed directly.
Chilkat SQL Server Downloads
--
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.
-- FullRequestNoBodyBd sends a request with no body and stores the binary response body in a
-- BinData. This is appropriate when the response is binary, such as an image or file download.
DECLARE @bdResponse int
EXEC @hr = sp_OACreate 'Chilkat.BinData', @bdResponse OUT
EXEC sp_OAMethod @rest, 'FullRequestNoBodyBd', @success OUT, 'GET', '/api/images/1', @bdResponse
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @rest
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 @bdResponse
RETURN
END
EXEC sp_OAGetProperty @bdResponse, 'NumBytes', @iTmp0 OUT
PRINT 'Downloaded ' + @iTmp0 + ' bytes.'
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @bdResponse
END
GO