Sample code for 30+ languages & platforms
SQL Server

curl with Path Variables and Query Param Variables

See more CURL Examples

This example demonstrates using variables located in the path and query params with the {{variable_name}} syntax.

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

    -- Variables can occur in the path and query params.  
    -- Variable names are enclosed between {{ and }}

    --  curl -X GET https://httpbin.org/{{verb}}?id={id_value}}
    DECLARE @targetCurl nvarchar(4000)
    SELECT @targetCurl = 'curl -X GET https://httpbin.org/{{verb}}?id={{id_value}}'

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

    -- Provide values for variables.
    -- In this example, "verb" is a path variable, and "id_value" is a query param variable.
    EXEC sp_OAMethod @httpCurl, 'SetVar', NULL, 'verb', 'get'
    EXEC sp_OAMethod @httpCurl, 'SetVar', NULL, 'id_value', '123'

    -- Run the curl command.
    EXEC sp_OAMethod @httpCurl, 'DoYourThing', @success OUT, @targetCurl
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @httpCurl, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @httpCurl
        RETURN
      END

    DECLARE @responseJson int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @responseJson OUT

    EXEC sp_OASetProperty @responseJson, 'EmitCompact', 0

    DECLARE @statusCode int
    EXEC sp_OAGetProperty @httpCurl, 'StatusCode', @statusCode OUT

    PRINT 'response status code: ' + @statusCode

    EXEC sp_OAMethod @httpCurl, 'GetResponseJson', @success OUT, @responseJson
    EXEC sp_OAMethod @responseJson, 'Emit', @sTmp0 OUT
    PRINT @sTmp0

    -- Output:

    -- response status code: 200
    -- {
    --   "args": {
    --     "id": "123"
    --   },
    --   "headers": {
    --     "Host": "httpbin.org",
    --     "X-Amzn-Trace-Id": "Root=1-69e92914-5d4136d240f2f7fe1056f126"
    --   },
    --   "origin": "222.222.222.222",
    --   "url": "https://httpbin.org/get?id=123"
    -- }

    EXEC @hr = sp_OADestroy @httpCurl
    EXEC @hr = sp_OADestroy @responseJson


END
GO