Chilkat Online Tools

SQL Server / Orchestrator / Machines - Create

Back to Collection Items

-- 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)
    -- This example assumes the Chilkat API to have been previously unlocked.
    -- See Global Unlock Sample for sample code.

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

    DECLARE @success int

    -- Use this online tool to generate code from sample JSON: Generate Code to Create JSON

    -- The following JSON is sent in the request body.

    -- {
    --   "Name": "SomeMachine",
    --   "Type": "Standard"
    -- }

    DECLARE @json int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.JsonObject', @json OUT

    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'Name', 'SomeMachine'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'Type', 'Standard'

    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'Content-Type', 'application/json'
    -- Adds the "Authorization: Bearer <access_token>" header.
    EXEC sp_OASetProperty @http, 'AuthToken', '<access_token>'
    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'X-UIPATH-OrganizationUnitId', '{{folderId}}'
    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'X-UIPATH-TenantName', '{{tenantName}}'

    DECLARE @resp int
    EXEC sp_OAMethod @http, 'PostJson3', @resp OUT, 'https://domain.com/odata/Machines', 'application/json', @json
    EXEC sp_OAGetProperty @http, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 = 0
      BEGIN
        EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @json
        RETURN
      END

    DECLARE @sbResponseBody int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.StringBuilder', @sbResponseBody OUT

    EXEC sp_OAMethod @resp, 'GetBodySb', @success OUT, @sbResponseBody

    DECLARE @jResp int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.JsonObject', @jResp OUT

    EXEC sp_OAMethod @jResp, 'LoadSb', @success OUT, @sbResponseBody
    EXEC sp_OASetProperty @jResp, 'EmitCompact', 0


    PRINT 'Response Body:'
    EXEC sp_OAMethod @jResp, 'Emit', @sTmp0 OUT
    PRINT @sTmp0

    DECLARE @respStatusCode int
    EXEC sp_OAGetProperty @resp, 'StatusCode', @respStatusCode OUT

    PRINT 'Response Status Code = ' + @respStatusCode
    IF @respStatusCode >= 400
      BEGIN

        PRINT 'Response Header:'
        EXEC sp_OAGetProperty @resp, 'Header', @sTmp0 OUT
        PRINT @sTmp0

        PRINT 'Failed.'
        EXEC @hr = sp_OADestroy @resp

        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @json
        EXEC @hr = sp_OADestroy @sbResponseBody
        EXEC @hr = sp_OADestroy @jResp
        RETURN
      END
    EXEC @hr = sp_OADestroy @resp

    -- Sample JSON response:
    -- (Sample code for parsing the JSON response is shown below)

    -- {
    --   "@odata.context": "https://platform.uipath.com/odata/$metadata#Machines/$entity",
    --   "LicenseKey": "765asd90-1c32-1b54-b6ed-d8f73jdk8",
    --   "Name": "SomeMachine",
    --   "Type": "Standard",
    --   "NonProductionSlots": 0,
    --   "UnattendedSlots": 0,
    --   "Id": 84312,
    --   "RobotVersions": [
    --   ]
    -- }

    -- Sample code for parsing the JSON response...
    -- Use this online tool to generate parsing code from sample JSON: Generate JSON Parsing Code

    DECLARE @odata_context nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @odata_context OUT, '"@odata.context"'
    DECLARE @LicenseKey nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @LicenseKey OUT, 'LicenseKey'
    DECLARE @Name nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Name OUT, 'Name'
    DECLARE @v_Type nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @v_Type OUT, 'Type'
    DECLARE @NonProductionSlots int
    EXEC sp_OAMethod @jResp, 'IntOf', @NonProductionSlots OUT, 'NonProductionSlots'
    DECLARE @UnattendedSlots int
    EXEC sp_OAMethod @jResp, 'IntOf', @UnattendedSlots OUT, 'UnattendedSlots'
    DECLARE @Id int
    EXEC sp_OAMethod @jResp, 'IntOf', @Id OUT, 'Id'
    DECLARE @i int
    SELECT @i = 0
    DECLARE @count_i int
    EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_i OUT, 'RobotVersions'
    WHILE @i < @count_i
      BEGIN
        EXEC sp_OASetProperty @jResp, 'I', @i
        SELECT @i = @i + 1
      END

    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @json
    EXEC @hr = sp_OADestroy @sbResponseBody
    EXEC @hr = sp_OADestroy @jResp


END
GO

Curl Command

curl -X POST
	-H "Authorization: Bearer <access_token>"
	-H "Content-Type: application/json"
	-H "X-UIPATH-TenantName: {{tenantName}}"
	-H "X-UIPATH-OrganizationUnitId: {{folderId}}"
	-d '{
  "Name": "SomeMachine",
  "Type": "Standard"
}'
https://domain.com/odata/Machines

Postman Collection Item JSON

{
  "name": "Machines - Create",
  "request": {
    "method": "POST",
    "header": [
      {
        "key": "Content-Type",
        "value": "application/json",
        "type": "text"
      },
      {
        "key": "X-UIPATH-TenantName",
        "value": "{{tenantName}}",
        "type": "text"
      },
      {
        "key": "X-UIPATH-OrganizationUnitId",
        "value": "{{folderId}}",
        "type": "text"
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "{\r\n  \"Name\": \"SomeMachine\",\r\n  \"Type\": \"Standard\"\r\n}"
    },
    "url": {
      "raw": "{{url}}/odata/Machines",
      "host": [
        "{{url}}"
      ],
      "path": [
        "odata",
        "Machines"
      ]
    }
  },
  "response": [
    {
      "name": "Machines - Create",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "name": "Content-Type",
            "value": "application/json",
            "type": "text"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\r\n  \"Name\": \"SomeMachine\",\r\n  \"Type\": \"Standard\"\r\n}"
        },
        "url": {
          "raw": "{{url}}/odata/Machines",
          "host": [
            "{{url}}"
          ],
          "path": [
            "odata",
            "Machines"
          ]
        }
      },
      "status": "Created",
      "code": 201,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Date",
          "value": "Tue, 29 Jan 2019 13:47:03 GMT"
        },
        {
          "key": "Content-Type",
          "value": "application/json; odata.metadata=minimal"
        },
        {
          "key": "Content-Length",
          "value": "245"
        },
        {
          "key": "Connection",
          "value": "keep-alive"
        },
        {
          "key": "Cache-Control",
          "value": "no-store, must-revalidate, no-cache, max-age=0"
        },
        {
          "key": "Location",
          "value": "https://platform.uipath.com/odata/Machines(84414)"
        },
        {
          "key": "Set-Cookie",
          "value": "; SameSite=lax"
        },
        {
          "key": "api-supported-versions",
          "value": "7.0"
        },
        {
          "key": "OData-Version",
          "value": "4.0"
        },
        {
          "key": "Request-Context",
          "value": "appId=cid-v1:d00ce63d-0891-41e4-a17c-25b2c5382e49"
        },
        {
          "key": "Access-Control-Expose-Headers",
          "value": "Request-Context"
        },
        {
          "key": "X-Content-Type-Options",
          "value": "nosniff"
        },
        {
          "key": "X-Frame-Options",
          "value": "DENY"
        },
        {
          "key": "Strict-Transport-Security",
          "value": "max-age=31536000; includeSubDomains"
        },
        {
          "key": "Expect-CT",
          "value": "max-age=604800, report-uri=\"https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct\""
        },
        {
          "key": "Server",
          "value": "cloudflare"
        },
        {
          "key": "CF-RAY",
          "value": "4a0c25f50cb92468-IAD"
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"@odata.context\": \"https://platform.uipath.com/odata/$metadata#Machines/$entity\",\n    \"LicenseKey\": \"765asd90-1c32-1b54-b6ed-d8f73jdk8\",\n    \"Name\": \"SomeMachine\",\n    \"Type\": \"Standard\",\n    \"NonProductionSlots\": 0,\n    \"UnattendedSlots\": 0,\n    \"Id\": 84312,\n    \"RobotVersions\": []\n}"
    }
  ]
}