SQL Server
SQL Server
REST Receive Response in Chunks
See more REST Examples
Demonstrates how to receive a REST HTTP response in chunks.Note: This example requires Chilkat 10.1.0 or greater.
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
-- Important: Do not use nvarchar(max). See the warning about using nvarchar(max).
DECLARE @sTmp0 nvarchar(4000)
DECLARE @success int
SELECT @success = 0
-- This example requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
DECLARE @rest int
EXEC @hr = sp_OACreate 'Chilkat.Rest', @rest OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
-- Connect to the web server
DECLARE @bTls int
SELECT @bTls = 1
DECLARE @port int
SELECT @port = 443
DECLARE @bAutoReconnect int
SELECT @bAutoReconnect = 1
EXEC sp_OAMethod @rest, 'Connect', @success OUT, 'chilkatsoft.com', @port, @bTls, @bAutoReconnect
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @rest
RETURN
END
-- Send the request.
-- This can be *any* kind of request: POST, GET, PUT, etc. using *any* of the Chilkat REST methods that send requests.
-- For this example, we'll just GET a simple XML document that is about 274K in size.
EXEC sp_OAMethod @rest, 'SendReqNoBody', @success OUT, 'GET', '/hamlet.xml'
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @rest
RETURN
END
-- Get the response header.
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
PRINT 'response status code = ' + @statusCode
DECLARE @outputFile nvarchar(4000)
SELECT @outputFile = 'c:/temp/qa_output/hamlet.xml'
DECLARE @fac int
EXEC @hr = sp_OACreate 'Chilkat.FileAccess', @fac OUT
EXEC sp_OAMethod @fac, 'OpenForWrite', @success OUT, @outputFile
IF @statusCode < 0
BEGIN
EXEC sp_OAGetProperty @fac, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @fac
RETURN
END
-- Get the response in chunks.
-- (Note: There are more efficient ways to simply download a file from a web server, such as by calling Chilkat's Http.Download method.
-- The purpose of this method is to show how to receive a response chunk-by-chunk.)
DECLARE @bd int
EXEC @hr = sp_OACreate 'Chilkat.BinData', @bd OUT
DECLARE @status int
SELECT @status = 1
WHILE @status = 1
BEGIN
-- Read a minimum of 16000 bytes.
-- Note: Because of TLS message lengths, or the possibility of the response being either compressed (gzip/deflate) or in the HTTP chunked encoding,
-- the amount of data received in each call can be greater than the specified min size.
-- Chilkat will return from the call as soon as it has received an amount equal to or more than the specified size,
-- except for the very last chunk, which can be less that the min size or even 0 bytes.
-- The status will be one of three values:
-- -1 = error
-- 0 = received the last chunk of the response.
-- 1 = received a chunk, and more chunks are coming..
-- The received data is *appended* to the contents of the BinData object.
EXEC sp_OAMethod @rest, 'ReadRespChunkBd', @status OUT, 16000, @bd
IF @status >= 0
BEGIN
EXEC sp_OAGetProperty @bd, 'NumBytes', @iTmp0 OUT
PRINT 'Received chunk: ' + @iTmp0 + ' bytes'
EXEC sp_OAMethod @fac, 'FileWriteBd', @success OUT, @bd, 0, 0
EXEC sp_OAMethod @bd, 'Clear', @success OUT
END
END
EXEC sp_OAMethod @fac, 'FileClose', NULL
PRINT 'Success.'
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @bd
END
GO