Sample code for 30+ languages & platforms
SQL Server

HTTPS multipart/form-data POST

See more HTTP Examples

Demonstrates how to send a multipart/form-data POST over HTTPS (using TLS).

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

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

    --  This example demonstrates how to send a multipart/form-data POST that
    --  looks like this:

    --  	POST /cgi/XXX.pl HTTP/1.0
    --  	Accept: text/html
    --  	Connection: Keep-Alive
    --  	User-Agent: XXX/8.0.15
    --  	Content-type: multipart/form-data, boundary=XXXxyxy
    --  	Content-Length: 682
    --  
    --  	--XXXxyxy
    --  	content-disposition: form-data; name="UploadAgent"
    --  
    --  	InterfaceVersion1.5
    --  	--XXXxyxy
    --  	content-disposition: form-data; name="user"
    --  
    --  	userValue
    --  	--XXXxyxy
    --  	content-disposition: form-data; name="password"
    --  
    --  	passwordValue
    --  	--XXXxyxy
    --  	content-disposition: form-data; name="file"
    --  
    --  	fileValue
    --  	--XXXxyxy
    --  	content-disposition: form-data; name="data_version"
    --  
    --  	dataVersion
    --  	--XXXxyxy
    --  	content-disposition: form-data; name="content2"; filename="XXX"
    --  
    --  	THE FILE CONTENT GOES HERE...
    --  	--XXXxyxy--
    --  

    --  First, let's build the HTTP request object
    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, 'Path', '/cgi/XXX.pl'

    --  The boundary string is automatically generated and added by Chilkat.
    --  The value for the boundary string doesn't matter. (As long as it's a unique string that doesn't occur elsewhere in the request.)
    EXEC sp_OASetProperty @req, 'ContentType', 'multipart/form-data'

    --  Adding the Connection: Keep-Alive is optional.  It only makes sense if the intent is to send
    --  additional requests to the same domain (your-namespace-sb.accesscontrol.windows.net) within a reasonable time period.
    EXEC sp_OAMethod @req, 'AddHeader', NULL, 'Connection', 'Keep-Alive'

    --  --------------------------------------------------
    --  IMPORTANT: Never set the Content-Length header.  
    --  Chilkat will automatically compute the correct Content-Length and will add it.
    --  --------------------------------------------------

    --  If a specific User-Agent header field is needed, it can be added by calling AddHeader.
    EXEC sp_OAMethod @req, 'AddHeader', NULL, 'User-Agent', 'XXX/8.0.15'

    --  The "Accept" header, if present, tells the server what Content-Type responses will be accepted.
    --  In this case, we're telling the server that we'll only accept "text/html" responses, and therefore
    --  the server SHOULD only send a text/html response.  Technically, the Accept header is not required.
    EXEC sp_OAMethod @req, 'AddHeader', NULL, 'Accept', 'text/html'

    --  Add the params to the request.  Given that the Content-Type is set to "multipart/form-data", when
    --  Chilkat composes the request, it will put each param in it's own MIME sub-part (i.e. in it's own
    --  part delimited by the boundary string).
    EXEC sp_OAMethod @req, 'AddParam', NULL, 'UploadAgent', 'InterfaceVersion1.5'
    EXEC sp_OAMethod @req, 'AddParam', NULL, 'user', 'userValue'
    EXEC sp_OAMethod @req, 'AddParam', NULL, 'password', 'passwordValue'
    EXEC sp_OAMethod @req, 'AddParam', NULL, 'file', 'fileValue'
    EXEC sp_OAMethod @req, 'AddParam', NULL, 'data_version', 'dataVersion'

    --  The last param is the contents of a file.
    --  If it's a file on disk, we can add it like this:
    DECLARE @pathToFileOnDisk nvarchar(4000)
    SELECT @pathToFileOnDisk = 'c:/someDir/someFile.dat'
    EXEC sp_OAMethod @req, 'AddFileForUpload', @success OUT, 'content2', @pathToFileOnDisk
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @req, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @req
        RETURN
      END

    --  Alternatively, if the contents of the file are in memory, perhaps in a string
    --  variable, the file can be added like this instead.
    DECLARE @fileContents nvarchar(4000)
    SELECT @fileContents = 'This is the content of the file being uploaded.'
    EXEC sp_OAMethod @req, 'AddStringForUpload', @success OUT, 'content2', 'XXX', @fileContents, 'utf-8'

    --  -----------------------------------------------------------
    --  IMPORTANT: To duplicate the HTTP request shown above, you'll want to choose 
    --  either AddStringForUpload or AddFileForUpload, but not both.  It's possible to upload
    --  any number of files by calling AddStringForUpload and/or AddFileForUpload any number
    --  of times, once per file to be uploaded.  This of course assumes that the receiving
    --  end is programmed to receive multiple files..
    --  ------------------------------------------------------------

    DECLARE @http int
    EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT

    --  The request is ready... now send it using HTTPS (which is port 443 by default).

    DECLARE @resp int
    EXEC @hr = sp_OACreate 'Chilkat.HttpResponse', @resp OUT

    EXEC sp_OAMethod @http, 'HttpSReq', @success OUT, 'www.myserver.com', 443, 1, @req, @resp
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @req
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @resp
        RETURN
      END


    EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
    PRINT 'HTTP response status: ' + @iTmp0

    --  In this case, the response would be HTML because our Accept header
    --  told the server to only return HTML.  The HTML is available on the BodyStr
    --  property of the response object:
    DECLARE @htmlStr nvarchar(4000)
    EXEC sp_OAGetProperty @resp, 'BodyStr', @htmlStr OUT

    PRINT 'Received:'

    PRINT @htmlStr

    EXEC @hr = sp_OADestroy @req
    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @resp


END
GO