Chilkat Online Tools

SQL Server / Creatio API / $batch parameter (Content-Type: application/json)

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.

    -- {
    --   "requests": [
    --     {
    --       "method": "POST",
    --       "url": "{{CollectionName5}}",
    --       "id": "t3",
    --       "body": {
    --         "{{FieldName1}}": "{{FieldName1Value4}}"
    --       },
    --       "headers": {
    --         "Content-Type": "application/json;odata=verbose",
    --         "Accept": "application/json;odata=verbose",
    --         "Prefer": "continue-on-error"
    --       }
    --     },
    --     {
    --       "method": "POST",
    --       "atomicityGroup": "g1",
    --       "url": "{{CollectionName5}}",
    --       "id": "t3",
    --       "body": {
    --         "{{FieldName6}}": "{{FieldName6Value1}}",
    --         "{{FieldName1}}": "{{FieldName1Value5}}"
    --       },
    --       "headers": {
    --         "Content-Type": "application/json;odata=verbose",
    --         "Accept": "application/json;odata=verbose",
    --         "Prefer": "continue-on-error"
    --       }
    --     },
    --     {
    --       "method": "PATCH",
    --       "atomicityGroup": "g1",
    --       "url": "{{CollectionName5}}/{{ObjectId4}}",
    --       "id": "t2",
    --       "body": {
    --         "{{FieldName1}}": "{{FieldName1Value6}}"
    --       },
    --       "headers": {
    --         "Content-Type": "application/json;odata=verbose",
    --         "Accept": "application/json;odata=verbose",
    --         "Prefer": "continue-on-error"
    --       }
    --     }
    --   ]
    -- }

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

    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'requests[0].method', 'POST'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'requests[0].url', '{{CollectionName5}}'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'requests[0].id', 't3'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'requests[0].body.{{FieldName1}}', '{{FieldName1Value4}}'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'requests[0].headers.Content-Type', 'application/json;odata=verbose'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'requests[0].headers.Accept', 'application/json;odata=verbose'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'requests[0].headers.Prefer', 'continue-on-error'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'requests[1].method', 'POST'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'requests[1].atomicityGroup', 'g1'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'requests[1].url', '{{CollectionName5}}'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'requests[1].id', 't3'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'requests[1].body.{{FieldName6}}', '{{FieldName6Value1}}'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'requests[1].body.{{FieldName1}}', '{{FieldName1Value5}}'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'requests[1].headers.Content-Type', 'application/json;odata=verbose'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'requests[1].headers.Accept', 'application/json;odata=verbose'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'requests[1].headers.Prefer', 'continue-on-error'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'requests[2].method', 'PATCH'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'requests[2].atomicityGroup', 'g1'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'requests[2].url', '{{CollectionName5}}/{{ObjectId4}}'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'requests[2].id', 't2'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'requests[2].body.{{FieldName1}}', '{{FieldName1Value6}}'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'requests[2].headers.Content-Type', 'application/json;odata=verbose'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'requests[2].headers.Accept', 'application/json;odata=verbose'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'requests[2].headers.Prefer', 'continue-on-error'

    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'Content-Type', 'application/json; odata=verbose; IEEE754Compatible=true'
    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'BPMCSRF', '{{BPMCSRF}}'
    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'Accept', 'application/json'
    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'ForceUseSession', 'true'

    DECLARE @resp int
    EXEC sp_OAMethod @http, 'PostJson3', @resp OUT, 'https://myserver.com/0/odata/$batch', 'application/json; odata=verbose; IEEE754Compatible=true', @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

    EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
    PRINT @iTmp0
    EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
    PRINT @sTmp0
    EXEC @hr = sp_OADestroy @resp


    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @json


END
GO

Curl Command

curl -X POST
	-H "Content-Type: application/json; odata=verbose; IEEE754Compatible=true"
	-H "Accept: application/json"
	-H "BPMCSRF: {{BPMCSRF}}"
	-H "ForceUseSession: true"
	-d '{
	"requests": [
	{
		"method": "POST",
		"url": "{{CollectionName5}}",
		"id": "t3",
		"body": {
			"{{FieldName1}}": "{{FieldName1Value4}}"
		},
		"headers": {
			"Content-Type": "application/json;odata=verbose",
			"Accept": "application/json;odata=verbose",
			"Prefer": "continue-on-error"
		}
	},
	{
		"method": "POST",
		"atomicityGroup": "g1",
		"url": "{{CollectionName5}}",
		"id": "t3",
		"body": {
			"{{FieldName6}}": "{{FieldName6Value1}}",
			"{{FieldName1}}": "{{FieldName1Value5}}"
		},
		"headers": {
			"Content-Type": "application/json;odata=verbose",
			"Accept": "application/json;odata=verbose",
			"Prefer": "continue-on-error"
		}
	},
	{
		"method": "PATCH",
		"atomicityGroup": "g1",
		"url": "{{CollectionName5}}/{{ObjectId4}}",
		"id": "t2",
		"body": {
			"{{FieldName1}}": "{{FieldName1Value6}}"
		},
		"headers": {
			"Content-Type": "application/json;odata=verbose",
			"Accept": "application/json;odata=verbose",
			"Prefer": "continue-on-error"
		}
	}
	]
}'
https://myserver.com/0/odata/$batch

Postman Collection Item JSON

{
  "name": "$batch parameter (Content-Type: application/json)",
  "request": {
    "method": "POST",
    "header": [
      {
        "key": "Content-Type",
        "name": "Content-Type",
        "type": "text",
        "value": "application/json; odata=verbose; IEEE754Compatible=true"
      },
      {
        "key": "Accept",
        "type": "text",
        "value": "application/json"
      },
      {
        "key": "BPMCSRF",
        "type": "text",
        "value": "{{BPMCSRF}}"
      },
      {
        "key": "ForceUseSession",
        "type": "text",
        "value": "true"
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "{\r\n\t\"requests\": [\r\n\t{\r\n\t\t\"method\": \"POST\",\r\n\t\t\"url\": \"{{CollectionName5}}\",\r\n\t\t\"id\": \"t3\",\r\n\t\t\"body\": {\r\n\t\t\t\"{{FieldName1}}\": \"{{FieldName1Value4}}\"\r\n\t\t},\r\n\t\t\"headers\": {\r\n\t\t\t\"Content-Type\": \"application/json;odata=verbose\",\r\n\t\t\t\"Accept\": \"application/json;odata=verbose\",\r\n\t\t\t\"Prefer\": \"continue-on-error\"\r\n\t\t}\r\n\t},\r\n\t{\r\n\t\t\"method\": \"POST\",\r\n\t\t\"atomicityGroup\": \"g1\",\r\n\t\t\"url\": \"{{CollectionName5}}\",\r\n\t\t\"id\": \"t3\",\r\n\t\t\"body\": {\r\n\t\t\t\"{{FieldName6}}\": \"{{FieldName6Value1}}\",\r\n\t\t\t\"{{FieldName1}}\": \"{{FieldName1Value5}}\"\r\n\t\t},\r\n\t\t\"headers\": {\r\n\t\t\t\"Content-Type\": \"application/json;odata=verbose\",\r\n\t\t\t\"Accept\": \"application/json;odata=verbose\",\r\n\t\t\t\"Prefer\": \"continue-on-error\"\r\n\t\t}\r\n\t},\r\n\t{\r\n\t\t\"method\": \"PATCH\",\r\n\t\t\"atomicityGroup\": \"g1\",\r\n\t\t\"url\": \"{{CollectionName5}}/{{ObjectId4}}\",\r\n\t\t\"id\": \"t2\",\r\n\t\t\"body\": {\r\n\t\t\t\"{{FieldName1}}\": \"{{FieldName1Value6}}\"\r\n\t\t},\r\n\t\t\"headers\": {\r\n\t\t\t\"Content-Type\": \"application/json;odata=verbose\",\r\n\t\t\t\"Accept\": \"application/json;odata=verbose\",\r\n\t\t\t\"Prefer\": \"continue-on-error\"\r\n\t\t}\r\n\t}\r\n\t]\r\n}",
      "options": {
        "raw": {
          "language": "json"
        }
      }
    },
    "url": {
      "raw": "{{BaseURI}}/0/odata/$batch",
      "host": [
        "{{BaseURI}}"
      ],
      "path": [
        "0",
        "odata",
        "$batch"
      ]
    },
    "description": "Batch request with the same type of returned content (uses the [$batch](http://docs.oasis-open.org/odata/odata/v4.01/odata-v4.01-part2-url-conventions.html#sec_AddressingtheBatchEndpointforaServic) parameter)."
  },
  "response": [
    {
      "name": "[200] Batch request with Content-Type: application/json",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "name": "Content-Type",
            "type": "text",
            "value": "application/json; odata=verbose; IEEE754Compatible=true"
          },
          {
            "key": "Accept",
            "type": "text",
            "value": "application/json"
          },
          {
            "key": "BPMCSRF",
            "type": "text",
            "value": "{{BPMCSRF}}"
          },
          {
            "key": "ForceUseSession",
            "type": "text",
            "value": "true"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\r\n\t\"requests\": [\r\n\t{\r\n\t\t\"method\": \"POST\",\r\n\t\t\"url\": \"City\",\r\n\t\t\"id\": \"t3\",\r\n\t\t\"body\": {\r\n\t\t\t\"Name\": \"Burbank\"\r\n\t\t},\r\n\t\t\"headers\": {\r\n\t\t\t\"Content-Type\": \"application/json;odata=verbose\",\r\n\t\t\t\"Accept\": \"application/json;odata=verbose\",\r\n\t\t\t\"Prefer\": \"continue-on-error\"\r\n\t\t}\r\n\t},\r\n\t{\r\n\t\t\"method\": \"POST\",\r\n\t\t\"atomicityGroup\": \"g1\",\r\n\t\t\"url\": \"City\",\r\n\t\t\"id\": \"t3\",\r\n\t\t\"body\": {\r\n\t\t\t\"Id\": \"62f9bc01-57cf-4cc7-90bf-8672acc922e3\",\r\n\t\t\t\"Name\": \"Spokane\"\r\n\t\t},\r\n\t\t\"headers\": {\r\n\t\t\t\"Content-Type\": \"application/json;odata=verbose\",\r\n\t\t\t\"Accept\": \"application/json;odata=verbose\",\r\n\t\t\t\"Prefer\": \"continue-on-error\"\r\n\t\t}\r\n\t},\r\n\t{\r\n\t\t\"method\": \"PATCH\",\r\n\t\t\"atomicityGroup\": \"g1\",\r\n\t\t\"url\": \"City/62f9bc01-57cf-4cc7-90bf-8672acc922e3\",\r\n\t\t\"id\": \"t2\",\r\n\t\t\"body\": {\r\n\t\t\t\"Name\": \"Texas\"\r\n\t\t},\r\n\t\t\"headers\": {\r\n\t\t\t\"Content-Type\": \"application/json;odata=verbose\",\r\n\t\t\t\"Accept\": \"application/json;odata=verbose\",\r\n\t\t\t\"Prefer\": \"continue-on-error\"\r\n\t\t}\r\n\t}\r\n\t]\r\n}",
          "options": {
            "raw": {
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "https://myserver.com/0/odata/$batch",
          "protocol": "https",
          "host": [
            "myserver",
            "com"
          ],
          "path": [
            "0",
            "odata",
            "$batch"
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "text/html"
        },
        {
          "key": "Server",
          "value": "Microsoft-IIS/10.0"
        },
        {
          "key": "Set-Cookie",
          "value": "BPMSESSIONID=bjh3kv1rgqozxjpcgfovrqyb; path=/0; secure; HttpOnly"
        },
        {
          "key": "X-Powered-By",
          "value": "ASP.NET"
        },
        {
          "key": "X-Frame-Options",
          "value": "SAMEORIGIN"
        },
        {
          "key": "X-Content-Type-Options",
          "value": "nosniff"
        },
        {
          "key": "Date",
          "value": "Mon, 18 May 2020 14:44:30 GMT"
        },
        {
          "key": "Content-Length",
          "value": "1233"
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"responses\": [\n        {\n            \"id\": \"t3\",\n            \"status\": 201,\n            \"headers\": {\n                \"location\": \"https://myserver.com/0/odata/City(b6a05348-55b1-4314-a228-436ba305d2f3)\",\n                \"content-type\": \"application/json; odata.metadata=minimal\",\n                \"odata-version\": \"4.0\"\n            },\n            \"body\": {\n                \"@odata.context\": \"https://myserver.com/0/odata/$metadata#City/$entity\",\n                \"Id\": \"b6a05348-55b1-4314-a228-436ba305d2f3\",\n                \"CreatedOn\": \"2020-05-18T17:50:39.2838673Z\",\n                \"CreatedById\": \"dad159f3-6c2d-446a-98d2-0f4d26662bbe\",\n                \"ModifiedOn\": \"2020-05-18T17:50:39.2838673Z\",\n                \"ModifiedById\": \"dad159f3-6c2d-446a-98d2-0f4d26662bbe\",\n                \"Name\": \"Burbank\",\n                \"Description\": \"\",\n                \"CountryId\": \"00000000-0000-0000-0000-000000000000\",\n                \"RegionId\": \"00000000-0000-0000-0000-000000000000\",\n                \"TimeZoneId\": \"00000000-0000-0000-0000-000000000000\",\n                \"ProcessListeners\": 0\n            }\n        },\n        {\n            \"id\": \"t3\",\n            \"atomicityGroup\": \"c59e36b2-2aa9-44fa-86d3-e7d68eecbfa0\",\n            \"status\": 201,\n            \"headers\": {\n                \"location\": \"https://myserver.com/0/odata/City(62f9bc01-57cf-4cc7-90bf-8672acc922e3)\",\n                \"content-type\": \"application/json; odata.metadata=minimal\",\n                \"odata-version\": \"4.0\"\n            },\n            \"body\": {\n                \"@odata.context\": \"https://myserver.com/CreatioWebApp/0/odata/$metadata#City/$entity\",\n                \"Id\": \"62f9bc01-57cf-4cc7-90bf-8672acc922e3\",\n                \"CreatedOn\": \"2020-05-18T17:50:39.361994Z\",\n                \"CreatedById\": \"dad159f3-6c2d-446a-98d2-0f4d26662bbe\",\n                \"ModifiedOn\": \"2020-05-18T17:50:39.361994Z\",\n                \"ModifiedById\": \"dad159f3-6c2d-446a-98d2-0f4d26662bbe\",\n                \"Name\": \"Spokane\",\n                \"Description\": \"\",\n                \"CountryId\": \"00000000-0000-0000-0000-000000000000\",\n                \"RegionId\": \"00000000-0000-0000-0000-000000000000\",\n                \"TimeZoneId\": \"00000000-0000-0000-0000-000000000000\",\n                \"ProcessListeners\": 0\n            }\n        },\n        {\n            \"id\": \"t2\",\n            \"atomicityGroup\": \"c59e36b2-2aa9-44fa-86d3-e7d68eecbfa0\",\n            \"status\": 204,\n            \"headers\": {\n                \"odata-version\": \"4.0\"\n            }\n        }\n    ]\n}"
    }
  ]
}