SQL Server
SQL Server
multipart/form-data HTTP POST
Demonstrates how to construct and send a multipart/form-data HTTP POST.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
-- 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.
-- Sends the following multipart/form-data POST:
-- 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--
--
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'
DECLARE @http int
EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
DECLARE @resp int
EXEC @hr = sp_OACreate 'Chilkat.HttpResponse', @resp OUT
EXEC sp_OAMethod @http, 'HttpSReq', @success OUT, 'example.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 @bd
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
RETURN
END
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
PRINT 'HTTP response status: ' + @iTmp0
PRINT 'Received:'
EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @req
EXEC @hr = sp_OADestroy @bd
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
END
GO