Sample code for 30+ languages & platforms
SQL Server

Add Data to multipart/form-data Request

See more HTTP Examples

Demonstrates how to add content (such as a JPG, PDF, XML, etc.) to multipart/form-data requests.

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 @success int
    SELECT @success = 0

    -- This example requires the Chilkat API to have been previously unlocked.
    -- See Global Unlock Sample for sample code.

    DECLARE @req int
    EXEC @hr = sp_OACreate 'Chilkat.HttpRequest', @req OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    EXEC sp_OASetProperty @req, 'HttpVerb', 'POST'
    EXEC sp_OASetProperty @req, 'ContentType', 'multipart/form-data'
    EXEC sp_OASetProperty @req, 'Path', '/xyz/something'

    EXEC sp_OAMethod @req, 'AddParam', NULL, 'param1', 'value1'
    EXEC sp_OAMethod @req, 'AddParam', NULL, 'param2', 'value2'

    -- Add some small files to the request. (Small so we can see what the full request looks like without too much data..)
    DECLARE @bd int
    EXEC @hr = sp_OACreate 'Chilkat.BinData', @bd OUT

    EXEC sp_OAMethod @bd, 'LoadFile', @success OUT, 'qa_data/jpg/starfish20.jpg'
    EXEC sp_OAMethod @req, 'AddBdForUpload', @success OUT, 'starfish20', 'starfish20.jpg', @bd, 'image/jpeg'

    EXEC sp_OAMethod @bd, 'LoadFile', @success OUT, 'qa_data/pdf/helloWorld.pdf'
    EXEC sp_OAMethod @req, 'AddBdForUpload', @success OUT, 'helloWorld', 'helloWorld.pdf', @bd, 'application/pdf'

    EXEC sp_OAMethod @bd, 'LoadFile', @success OUT, 'qa_data/xml/tinyA.xml'
    EXEC sp_OAMethod @req, 'AddBdForUpload', @success OUT, 'tinyA', 'tinyA.xml', @bd, 'text/xml'

    -- This HTTP request contains binary data, so we cannot call GenerateRequestText
    -- to examine it as a string.  We can instead call GenerateRequestFile
    -- to write the request to a file and then examine using a hex editor,
    -- or an editor that is capable of handling null bytes and non-printable chars (perhaps by displaying "?" chars instead).
    EXEC sp_OAMethod @req, 'GenerateRequestFile', @success OUT, 'qa_output/multipartFormDataRequest.bin'


    PRINT 'OK.'

    -- This is the HTTP multipart/form-data request built by the above code:

    -- POST /xyz/something HTTP/1.1
    -- Host: domain
    -- Content-Type: multipart/form-data; boundary=------------090708030009010000030901
    -- Content-Length: 2220
    -- 
    -- --------------090708030009010000030901
    -- Content-Disposition: form-data; name="param1"
    -- 
    -- value1
    -- --------------090708030009010000030901
    -- Content-Disposition: form-data; name="param2"
    -- 
    -- value2
    -- --------------090708030009010000030901
    -- Content-Disposition: form-data; name="starfish20"; filename="starfish20.jpg"
    -- Content-Type: image/jpeg
    -- 
    -- JPEG DATA HERE...
    -- --------------090708030009010000030901
    -- Content-Disposition: form-data; name="helloWorld"; filename="helloWorld.pdf"
    -- Content-Type: application/pdf
    -- 
    -- PDF DATA HERE...
    -- --------------090708030009010000030901
    -- Content-Disposition: form-data; name="tinyA"; filename="tinyA.xml"
    -- Content-Type: text/xml
    -- 
    -- XML DATA HERE...
    -- --------------090708030009010000030901--
    -- 

    EXEC @hr = sp_OADestroy @req
    EXEC @hr = sp_OADestroy @bd


END
GO