Sample code for 30+ languages & platforms
SQL Server

Set a Multipart Part Body from a Stream

See more REST Examples

Demonstrates Rest.SetMultipartBodyStream, which uses a Stream as the body source for the selected multipart part.

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 a part body is appropriate for large parts because the data is read incrementally from the source rather than being buffered entirely in memory. The part is otherwise assembled the same way — select the part, set its headers, then set its body.

Chilkat SQL Server Downloads

SQL Server
-- 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.

    --  Select the multipart part to build, and set its header fields.
    EXEC sp_OASetProperty @rest, 'PartSelector', '1'
    EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'Content-Disposition', 'form-data; name="field1"'
    EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'Content-Type', 'application/octet-stream'

    --  Use a Stream as the body source for the selected part.  The stream's source is a local file,
    --  which is appropriate for large parts.
    DECLARE @partStream int
    EXEC @hr = sp_OACreate 'Chilkat.Stream', @partStream OUT

    EXEC sp_OASetProperty @partStream, 'SourceFile', 'qa_data/attachment.bin'
    EXEC sp_OAMethod @rest, 'SetMultipartBodyStream', @success OUT, @partStream
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @partStream
        RETURN
      END

    DECLARE @responseText nvarchar(4000)
    EXEC sp_OAMethod @rest, 'FullRequestMultipart', @responseText OUT, 'POST', '/api/upload'
    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 @partStream
        RETURN
      END

    PRINT @responseText

    EXEC @hr = sp_OADestroy @rest
    EXEC @hr = sp_OADestroy @partStream


END
GO