SQL Server
SQL Server
Send a REST Request with a Streamed Body
See more REST Examples
Demonstrates Rest.FullRequestStream, which sends a complete request whose body is read from a Stream, then returns the response body as text.
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. Streaming the body is appropriate for large uploads because the data is read incrementally from the source (here a file) rather than being held entirely in memory.
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.
-- The request body is read from a Stream. Here the stream's source is a local file, which is
-- appropriate for large uploads because the data need not be held entirely in memory.
DECLARE @bodyStream int
EXEC @hr = sp_OACreate 'Chilkat.Stream', @bodyStream OUT
EXEC sp_OASetProperty @bodyStream, 'SourceFile', 'qa_data/upload.json'
EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'Content-Type', 'application/json'
-- The 3rd argument is the Stream that supplies the request body.
DECLARE @responseText nvarchar(4000)
EXEC sp_OAMethod @rest, 'FullRequestStream', @responseText OUT, 'POST', '/api/upload', @bodyStream
EXEC sp_OAGetProperty @rest, 'LastMethodSuccess', @iTmp0 OUT
IF @iTmp0 = 0
BEGIN
EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @bodyStream
RETURN
END
PRINT @responseText
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @bodyStream
END
GO