SQL Server
SQL Server
Read the REST Response Body in Chunks
See more REST Examples
Demonstrates Rest.ReadRespChunkBd, which reads the response body one chunk at a time, appending each chunk to a BinData. A return value of 0 signals the body is complete, while -1 indicates a read failure.
Background. Chunked reading lets an application process a large response incrementally rather than buffering the entire body at once. It is used after ReadResponseHeader within the staged request model.
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
EXEC sp_OAMethod @rest, 'SendReqNoBody', @success OUT, 'GET', '/api/largefile'
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 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 in chunks, appending each chunk to the BinData.
-- Each call waits until at least the requested number of bytes is available, unless the response
-- ends first. A return value of 0 means the final bytes were returned and the body is complete;
-- -1 indicates a read failure.
DECLARE @bdBody int
EXEC @hr = sp_OACreate 'Chilkat.BinData', @bdBody OUT
DECLARE @chunkResult int
EXEC sp_OAMethod @rest, 'ReadRespChunkBd', @chunkResult OUT, 8192, @bdBody
WHILE @chunkResult > 0
BEGIN
EXEC sp_OAMethod @rest, 'ReadRespChunkBd', @chunkResult OUT, 8192, @bdBody
END
IF @chunkResult < 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 @bdBody
RETURN
END
EXEC sp_OAGetProperty @bdBody, 'NumBytes', @iTmp0 OUT
PRINT 'Received ' + @iTmp0 + ' bytes.'
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @sbError
EXEC @hr = sp_OADestroy @bdBody
END
GO