Chilkat Online Tools

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

Back to Collection Items

Use ChilkatAx-win32.pkg

Procedure Test
    Handle hoHttp
    Boolean iSuccess
    Variant vJson
    Handle hoJson
    Variant vResp
    Handle hoResp
    String sTemp1
    Integer iTemp1
    Boolean bTemp1

    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    Get Create (RefClass(cComChilkatHttp)) To hoHttp
    If (Not(IsComObjectCreated(hoHttp))) Begin
        Send CreateComObject of hoHttp
    End

    // 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"
    //       }
    //     }
    //   ]
    // }

    Get Create (RefClass(cComChilkatJsonObject)) To hoJson
    If (Not(IsComObjectCreated(hoJson))) Begin
        Send CreateComObject of hoJson
    End
    Get ComUpdateString Of hoJson "requests[0].method" "POST" To iSuccess
    Get ComUpdateString Of hoJson "requests[0].url" "{{CollectionName5}}" To iSuccess
    Get ComUpdateString Of hoJson "requests[0].id" "t3" To iSuccess
    Get ComUpdateString Of hoJson "requests[0].body.{{FieldName1}}" "{{FieldName1Value4}}" To iSuccess
    Get ComUpdateString Of hoJson "requests[0].headers.Content-Type" "application/json;odata=verbose" To iSuccess
    Get ComUpdateString Of hoJson "requests[0].headers.Accept" "application/json;odata=verbose" To iSuccess
    Get ComUpdateString Of hoJson "requests[0].headers.Prefer" "continue-on-error" To iSuccess
    Get ComUpdateString Of hoJson "requests[1].method" "POST" To iSuccess
    Get ComUpdateString Of hoJson "requests[1].atomicityGroup" "g1" To iSuccess
    Get ComUpdateString Of hoJson "requests[1].url" "{{CollectionName5}}" To iSuccess
    Get ComUpdateString Of hoJson "requests[1].id" "t3" To iSuccess
    Get ComUpdateString Of hoJson "requests[1].body.{{FieldName6}}" "{{FieldName6Value1}}" To iSuccess
    Get ComUpdateString Of hoJson "requests[1].body.{{FieldName1}}" "{{FieldName1Value5}}" To iSuccess
    Get ComUpdateString Of hoJson "requests[1].headers.Content-Type" "application/json;odata=verbose" To iSuccess
    Get ComUpdateString Of hoJson "requests[1].headers.Accept" "application/json;odata=verbose" To iSuccess
    Get ComUpdateString Of hoJson "requests[1].headers.Prefer" "continue-on-error" To iSuccess
    Get ComUpdateString Of hoJson "requests[2].method" "PATCH" To iSuccess
    Get ComUpdateString Of hoJson "requests[2].atomicityGroup" "g1" To iSuccess
    Get ComUpdateString Of hoJson "requests[2].url" "{{CollectionName5}}/{{ObjectId4}}" To iSuccess
    Get ComUpdateString Of hoJson "requests[2].id" "t2" To iSuccess
    Get ComUpdateString Of hoJson "requests[2].body.{{FieldName1}}" "{{FieldName1Value6}}" To iSuccess
    Get ComUpdateString Of hoJson "requests[2].headers.Content-Type" "application/json;odata=verbose" To iSuccess
    Get ComUpdateString Of hoJson "requests[2].headers.Accept" "application/json;odata=verbose" To iSuccess
    Get ComUpdateString Of hoJson "requests[2].headers.Prefer" "continue-on-error" To iSuccess

    Send ComSetRequestHeader To hoHttp "Content-Type" "application/json; odata=verbose; IEEE754Compatible=true"
    Send ComSetRequestHeader To hoHttp "BPMCSRF" "{{BPMCSRF}}"
    Send ComSetRequestHeader To hoHttp "Accept" "application/json"
    Send ComSetRequestHeader To hoHttp "ForceUseSession" "true"

    Get pvComObject of hoJson to vJson
    Get ComPostJson3 Of hoHttp "https://myserver.com/0/odata/$batch" "application/json; odata=verbose; IEEE754Compatible=true" vJson To vResp
    If (IsComObject(vResp)) Begin
        Get Create (RefClass(cComChilkatHttpResponse)) To hoResp
        Set pvComObject Of hoResp To vResp
    End
    Get ComLastMethodSuccess Of hoHttp To bTemp1
    If (bTemp1 = False) Begin
        Get ComLastErrorText Of hoHttp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComStatusCode Of hoResp To iTemp1
    Showln iTemp1
    Get ComBodyStr Of hoResp To sTemp1
    Showln sTemp1
    Send Destroy of hoResp


End_Procedure

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}"
    }
  ]
}