Chilkat Online Tools

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

Back to Collection Items

#import <CkoHttp.h>
#import <CkoJsonObject.h>
#import <CkoHttpResponse.h>

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

CkoHttp *http = [[CkoHttp alloc] init];
BOOL success;

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

CkoJsonObject *json = [[CkoJsonObject alloc] init];
[json UpdateString: @"requests[0].method" value: @"POST"];
[json UpdateString: @"requests[0].url" value: @"{{CollectionName5}}"];
[json UpdateString: @"requests[0].id" value: @"t3"];
[json UpdateString: @"requests[0].body.{{FieldName1}}" value: @"{{FieldName1Value4}}"];
[json UpdateString: @"requests[0].headers.Content-Type" value: @"application/json;odata=verbose"];
[json UpdateString: @"requests[0].headers.Accept" value: @"application/json;odata=verbose"];
[json UpdateString: @"requests[0].headers.Prefer" value: @"continue-on-error"];
[json UpdateString: @"requests[1].method" value: @"POST"];
[json UpdateString: @"requests[1].atomicityGroup" value: @"g1"];
[json UpdateString: @"requests[1].url" value: @"{{CollectionName5}}"];
[json UpdateString: @"requests[1].id" value: @"t3"];
[json UpdateString: @"requests[1].body.{{FieldName6}}" value: @"{{FieldName6Value1}}"];
[json UpdateString: @"requests[1].body.{{FieldName1}}" value: @"{{FieldName1Value5}}"];
[json UpdateString: @"requests[1].headers.Content-Type" value: @"application/json;odata=verbose"];
[json UpdateString: @"requests[1].headers.Accept" value: @"application/json;odata=verbose"];
[json UpdateString: @"requests[1].headers.Prefer" value: @"continue-on-error"];
[json UpdateString: @"requests[2].method" value: @"PATCH"];
[json UpdateString: @"requests[2].atomicityGroup" value: @"g1"];
[json UpdateString: @"requests[2].url" value: @"{{CollectionName5}}/{{ObjectId4}}"];
[json UpdateString: @"requests[2].id" value: @"t2"];
[json UpdateString: @"requests[2].body.{{FieldName1}}" value: @"{{FieldName1Value6}}"];
[json UpdateString: @"requests[2].headers.Content-Type" value: @"application/json;odata=verbose"];
[json UpdateString: @"requests[2].headers.Accept" value: @"application/json;odata=verbose"];
[json UpdateString: @"requests[2].headers.Prefer" value: @"continue-on-error"];

[http SetRequestHeader: @"Content-Type" value: @"application/json; odata=verbose; IEEE754Compatible=true"];
[http SetRequestHeader: @"BPMCSRF" value: @"{{BPMCSRF}}"];
[http SetRequestHeader: @"Accept" value: @"application/json"];
[http SetRequestHeader: @"ForceUseSession" value: @"true"];

CkoHttpResponse *resp = [http PostJson3: @"https://myserver.com/0/odata/$batch" contentType: @"application/json; odata=verbose; IEEE754Compatible=true" json: json];
if (http.LastMethodSuccess == NO) {
    NSLog(@"%@",http.LastErrorText);
    return;
}

NSLog(@"%d",[resp.StatusCode intValue]);
NSLog(@"%@",resp.BodyStr);

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