Sample code for 30+ languages & platforms
SQL Server

REST Send multipart/form-data

See more REST Examples

Demonstrates how to send a multipart/form-data HTTP request.

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
    -- 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 destination 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, 'www.somewebserver.com', @port, @bTls, @bAutoReconnect

    -- This example will send the following multipart/form-data request.
    -- The Content-Length is automatically computed and added by Chilkat.
    -- It will be different than what is shown here.
    -- The boundary string is also generated by Chilkat and will be different
    -- than shown below.

    -- 	POST /some_path HTTP/1.1
    -- 	Content-Type: multipart/form-data; boundary=---------------------------735323031399963166993862150
    -- 	Content-Length: 834
    -- 
    -- 	-----------------------------735323031399963166993862150
    -- 	Content-Disposition: form-data; name="text1"
    -- 
    -- 	text 123 abc
    -- 	-----------------------------735323031399963166993862150
    -- 	Content-Disposition: form-data; name="text2"
    -- 
    -- 	xyz
    -- 	-----------------------------735323031399963166993862150
    -- 	Content-Disposition: form-data; name="file1"; filename="a.txt"
    -- 	Content-Type: text/plain
    -- 
    -- 	Content of a.txt.
    -- 
    -- 	-----------------------------735323031399963166993862150
    -- 	Content-Disposition: form-data; name="file2"; filename="a.html"
    -- 	Content-Type: text/html
    -- 
    -- 	<!DOCTYPE html><title>Content of a.html.</title>
    -- 
    -- 	-----------------------------735323031399963166993862150
    -- 	Content-Disposition: form-data; name="file3"; filename="starfish.jpg"
    -- 	Content-Type: image/jpeg
    -- 
    -- 	binary data goes here
    -- 	-----------------------------735323031399963166993862150--

    -- Set the Content-Type for the topmost MIME part.
    EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'Content-Type', 'multipart/form-data'

    -- Specify each part of the request.
    EXEC sp_OASetProperty @rest, 'PartSelector', '1'
    EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'Content-Disposition', 'form-data; name="text1"'
    EXEC sp_OAMethod @rest, 'SetMultipartBodyString', @success OUT, 'text 123 abc'

    EXEC sp_OASetProperty @rest, 'PartSelector', '2'
    EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'Content-Disposition', 'form-data; name="text2"'
    EXEC sp_OAMethod @rest, 'SetMultipartBodyString', @success OUT, 'xyz'

    EXEC sp_OASetProperty @rest, 'PartSelector', '3'
    EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'Content-Disposition', 'form-data; name="file1"; filename="a.txt"'
    EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'Content-Type', 'text/plain'
    EXEC sp_OAMethod @rest, 'SetMultipartBodyString', @success OUT, 'Content of a.txt.'

    EXEC sp_OASetProperty @rest, 'PartSelector', '4'
    EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'Content-Disposition', 'form-data; name="file2"; filename="a.html"'
    EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'Content-Type', 'text/html'
    DECLARE @sbHtml int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbHtml OUT

    EXEC sp_OAMethod @sbHtml, 'LoadFile', @success OUT, 'qa_data/html/a.html', 'utf-8'
    EXEC sp_OAMethod @rest, 'SetMultipartBodySb', @success OUT, @sbHtml

    EXEC sp_OASetProperty @rest, 'PartSelector', '5'
    EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'Content-Disposition', 'form-data; name="file3"; filename="starfish.jpg"'
    EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'Content-Type', 'image/jpeg'
    DECLARE @jpgBytes int
    EXEC @hr = sp_OACreate 'Chilkat.BinData', @jpgBytes OUT

    EXEC sp_OAMethod @jpgBytes, 'LoadFile', @success OUT, 'qa_data/jpg/starfish.jpg'
    EXEC sp_OAMethod @rest, 'SetMultipartBodyBd', @success OUT, @jpgBytes

    DECLARE @responseBody nvarchar(4000)
    EXEC sp_OAMethod @rest, 'FullRequestMultipart', @responseBody OUT, 'POST', '/some_path'
    EXEC sp_OAGetProperty @rest, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 <> 1
      BEGIN
        EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @sbHtml
        EXEC @hr = sp_OADestroy @jpgBytes
        RETURN
      END

    -- ...
    -- ...

    -- Clear the REST object for any subsequent requests..
    EXEC sp_OAMethod @rest, 'ClearAllHeaders', @success OUT
    EXEC sp_OAMethod @rest, 'ClearAllParts', @success OUT
    EXEC sp_OASetProperty @rest, 'PartSelector', ''

    EXEC @hr = sp_OADestroy @rest
    EXEC @hr = sp_OADestroy @sbHtml
    EXEC @hr = sp_OADestroy @jpgBytes


END
GO