Sample code for 30+ languages & platforms
SQL Server

curl with Variable Substitution in an XML Request Body

See more CURL Examples

This example shows how to use variables inside an XML request body using the {{variable_name}} syntax. When the HTTP request’s Content-Type indicates XML, Chilkat automatically applies proper XML entity encoding to each substituted value, ensuring the resulting XML remains valid.

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
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    -- Variable names are enclosed between {{ and }}

    -- curl -X POST https://api.example.com/api/orders \
    --   -H "Content-Type: application/xml; charset=utf-8" \
    --   -H "Accept: application/xml" \
    --   -d '<order>
    --   <customerName>{{customer_name}}</customerName>
    --   <note>{{note}}</note>
    --   <address>{{address}}</address>
    --   <instructions>{{instructions}}</instructions>
    -- </order>'

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

    EXEC sp_OAMethod @sbCurl, 'AppendLn', @success OUT, 'curl -X POST https://api.example.com/api/orders \'
    EXEC sp_OAMethod @sbCurl, 'AppendLn', @success OUT, '  -H "Content-Type: application/xml; charset=utf-8" \'
    EXEC sp_OAMethod @sbCurl, 'AppendLn', @success OUT, '  -H "Accept: application/xml" \'
    EXEC sp_OAMethod @sbCurl, 'AppendLn', @success OUT, '  -d ''<order>'
    EXEC sp_OAMethod @sbCurl, 'AppendLn', @success OUT, '  <customerName>{{customer_name}}</customerName>'
    EXEC sp_OAMethod @sbCurl, 'AppendLn', @success OUT, '  <note>{{note}}</note>'
    EXEC sp_OAMethod @sbCurl, 'AppendLn', @success OUT, '  <address>{{address}}</address>'
    EXEC sp_OAMethod @sbCurl, 'AppendLn', @success OUT, '  <instructions>{{instructions}}</instructions>'
    EXEC sp_OAMethod @sbCurl, 'AppendLn', @success OUT, '</order>'''

    DECLARE @curl int
    EXEC @hr = sp_OACreate 'Chilkat.HttpCurl', @curl OUT

    -- The values below contain chars that will require XML entity encoding.  
    -- Chilkat will automatically do the encoding because the Content-Type of this request is "application/xml"
    EXEC sp_OAMethod @curl, 'SetVar', NULL, 'customer_name', 'John & Sons'
    EXEC sp_OAMethod @curl, 'SetVar', NULL, 'note', 'He said "Ship it ASAP!"'
    EXEC sp_OAMethod @curl, 'SetVar', NULL, 'address', '123 <Main> Street'
    EXEC sp_OAMethod @curl, 'SetVar', NULL, 'instructions', 'Use door #2 & call upon arrival'

    -- To demonstrate how the variables are replaced, this example does not execute the curl command. 
    -- Instead, it generates the raw HTTP request that would be sent if the curl command were run.
    DECLARE @sbRawRequest int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbRawRequest OUT

    EXEC sp_OAMethod @sbCurl, 'GetAsString', @sTmp0 OUT
    EXEC sp_OAMethod @curl, 'ToRawRequest', @success OUT, @sTmp0, @sbRawRequest
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @curl, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @sbCurl
        EXEC @hr = sp_OADestroy @curl
        EXEC @hr = sp_OADestroy @sbRawRequest
        RETURN
      END

    EXEC sp_OAMethod @sbRawRequest, 'GetAsString', @sTmp0 OUT
    PRINT @sTmp0

    -- The output is shown below.
    -- Notice the chars that were XML entity encoded.

    -- POST /api/orders HTTP/1.1
    -- Accept: application/xml
    -- Host: api.example.com
    -- Content-Type: application/xml; charset=utf-8
    -- Content-Length: 229
    -- 
    -- <order>
    --   <customerName>John &amp; Sons</customerName>
    --   <note>He said &quot;Ship it ASAP!&quot;</note>
    --   <address>123 &lt;Main&gt; Street</address>
    --   <instructions>Use door #2 &amp; call upon arrival</instructions>
    -- </order>

    EXEC @hr = sp_OADestroy @sbCurl
    EXEC @hr = sp_OADestroy @curl
    EXEC @hr = sp_OADestroy @sbRawRequest


END
GO