SQL Server
SQL Server
Send a REST Request with a Binary Body
See more REST Examples
Demonstrates Rest.FullRequestBd, which sends a complete request whose binary body is read from a BinData and stores the text response body in a StringBuilder.
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. FullRequestBd is used when the request payload is binary, such as an image or other file, while the response is textual (for example a JSON acknowledgement).
Chilkat SQL Server Downloads
-- 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.
-- FullRequestBd sends a request whose binary body is read from a BinData and stores the text
-- response body in a StringBuilder.
DECLARE @bdRequest int
EXEC @hr = sp_OACreate 'Chilkat.BinData', @bdRequest OUT
DECLARE @bLoaded int
EXEC sp_OAMethod @bdRequest, 'LoadFile', @bLoaded OUT, 'qa_data/image.png'
IF @bLoaded <> 1
BEGIN
PRINT 'Failed to load the request body file.'
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @bdRequest
RETURN
END
EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'Content-Type', 'image/png'
DECLARE @sbResponse int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbResponse OUT
EXEC sp_OAMethod @rest, 'FullRequestBd', @success OUT, 'PUT', '/api/images/1', @bdRequest, @sbResponse
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @bdRequest
EXEC @hr = sp_OADestroy @sbResponse
RETURN
END
DECLARE @responseText nvarchar(4000)
EXEC sp_OAMethod @sbResponse, 'GetAsString', @responseText OUT
EXEC sp_OAGetProperty @sbResponse, 'LastMethodSuccess', @iTmp0 OUT
IF @iTmp0 = 0
BEGIN
EXEC sp_OAGetProperty @sbResponse, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @bdRequest
EXEC @hr = sp_OADestroy @sbResponse
RETURN
END
PRINT @responseText
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @bdRequest
EXEC @hr = sp_OADestroy @sbResponse
END
GO