Chilkat Online Tools

unicodeCpp / Support API / Create Automation

Back to Collection Items

#include <CkHttpW.h>
#include <CkHttpResponseW.h>
#include <CkStringBuilderW.h>
#include <CkJsonObjectW.h>

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

    CkHttpW http;
    bool success;

    http.put_BasicAuth(true);
    http.put_Login(L"login");
    http.put_Password(L"password");

    http.SetRequestHeader(L"Accept",L"application/json");

    CkHttpResponseW *resp = http.QuickRequest(L"POST",L"https://example.zendesk.com/api/v2/automations");
    if (http.get_LastMethodSuccess() == false) {
        wprintf(L"%s\n",http.lastErrorText());
        return;
    }

    CkStringBuilderW sbResponseBody;
    resp->GetBodySb(sbResponseBody);

    CkJsonObjectW jResp;
    jResp.LoadSb(sbResponseBody);
    jResp.put_EmitCompact(false);

    wprintf(L"Response Body:\n");
    wprintf(L"%s\n",jResp.emit());

    int respStatusCode = resp->get_StatusCode();
    wprintf(L"Response Status Code = %d\n",respStatusCode);
    if (respStatusCode >= 400) {
        wprintf(L"Response Header:\n");
        wprintf(L"%s\n",resp->header());
        wprintf(L"Failed.\n");
        delete resp;
        return;
    }

    delete resp;

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

    // {
    //   "automation": {
    //     "actions": [
    //       {
    //         "field": "<string>",
    //         "value": "<string>"
    //       },
    //       {
    //         "field": "<string>",
    //         "value": "<string>"
    //       }
    //     ],
    //     "active": "<boolean>",
    //     "conditions": {
    //       "all": [
    //         {
    //           "field": "<string>",
    //           "operator": "<string>",
    //           "value": "<string>"
    //         },
    //         {
    //           "field": "<string>",
    //           "operator": "<string>",
    //           "value": "<string>"
    //         }
    //       ],
    //       "any": [
    //         {
    //           "field": "<string>",
    //           "operator": "<string>",
    //           "value": "<string>"
    //         },
    //         {
    //           "field": "<string>",
    //           "operator": "<string>",
    //           "value": "<string>"
    //         }
    //       ]
    //     },
    //     "created_at": "<dateTime>",
    //     "default": "<boolean>",
    //     "id": "<integer>",
    //     "position": "<integer>",
    //     "raw_title": "<string>",
    //     "title": "<string>",
    //     "updated_at": "<dateTime>"
    //   }
    // }

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

    // Chilkat functions returning "const char *" return a pointer to temporary internal memory owned and managed by Chilkat.

    const wchar_t *field = 0;
    const wchar_t *value = 0;
    const wchar_t *operator = 0;

    const wchar_t *Active = jResp.stringOf(L"automation.active");
    const wchar_t *Created_at = jResp.stringOf(L"automation.created_at");
    const wchar_t *Default = jResp.stringOf(L"automation.default");
    const wchar_t *Id = jResp.stringOf(L"automation.id");
    const wchar_t *Position = jResp.stringOf(L"automation.position");
    const wchar_t *Raw_title = jResp.stringOf(L"automation.raw_title");
    const wchar_t *Title = jResp.stringOf(L"automation.title");
    const wchar_t *Updated_at = jResp.stringOf(L"automation.updated_at");
    int i = 0;
    int count_i = jResp.SizeOfArray(L"automation.actions");
    while (i < count_i) {
        jResp.put_I(i);
        field = jResp.stringOf(L"automation.actions[i].field");
        value = jResp.stringOf(L"automation.actions[i].value");
        i = i + 1;
    }

    i = 0;
    count_i = jResp.SizeOfArray(L"automation.conditions.all");
    while (i < count_i) {
        jResp.put_I(i);
        field = jResp.stringOf(L"automation.conditions.all[i].field");
        operator = jResp.stringOf(L"automation.conditions.all[i].operator");
        value = jResp.stringOf(L"automation.conditions.all[i].value");
        i = i + 1;
    }

    i = 0;
    count_i = jResp.SizeOfArray(L"automation.conditions.any");
    while (i < count_i) {
        jResp.put_I(i);
        field = jResp.stringOf(L"automation.conditions.any[i].field");
        operator = jResp.stringOf(L"automation.conditions.any[i].operator");
        value = jResp.stringOf(L"automation.conditions.any[i].value");
        i = i + 1;
    }
    }

Curl Command

curl  -u login:password -X POST
	-H "Accept: application/json"
https://example.zendesk.com/api/v2/automations

Postman Collection Item JSON

{
  "name": "Create Automation",
  "request": {
    "method": "POST",
    "header": [
      {
        "key": "Accept",
        "value": "application/json"
      }
    ],
    "url": {
      "raw": "{{baseUrl}}/api/v2/automations",
      "host": [
        "{{baseUrl}}"
      ],
      "path": [
        "api",
        "v2",
        "automations"
      ]
    },
    "description": "Creates an automation.\n\nNew automations must be unique and have at least one condition that is true only once or an action that nullifies at least one of the conditions. Active automations can have overlapping conditions but can't be identical.\n\nThe request must include the following conditions in the `all` array:\n\n- At least one time-based condition\n- At least one condition that checks one of the following fields: `status`, `type`, `group_id`, `assignee_id`, or `requester_id`.\n\n#### Allowed For\n\n* Agents\n"
  },
  "response": [
    {
      "name": "Created response",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "description": "Added as a part of security scheme: basic",
            "key": "Authorization",
            "value": "Basic <credentials>"
          }
        ],
        "url": {
          "raw": "{{baseUrl}}/api/v2/automations",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "api",
            "v2",
            "automations"
          ]
        }
      },
      "status": "Created",
      "code": 201,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n  \"automation\": {\n    \"actions\": [\n      {\n        \"field\": \"<string>\",\n        \"value\": \"<string>\"\n      },\n      {\n        \"field\": \"<string>\",\n        \"value\": \"<string>\"\n      }\n    ],\n    \"active\": \"<boolean>\",\n    \"conditions\": {\n      \"all\": [\n        {\n          \"field\": \"<string>\",\n          \"operator\": \"<string>\",\n          \"value\": \"<string>\"\n        },\n        {\n          \"field\": \"<string>\",\n          \"operator\": \"<string>\",\n          \"value\": \"<string>\"\n        }\n      ],\n      \"any\": [\n        {\n          \"field\": \"<string>\",\n          \"operator\": \"<string>\",\n          \"value\": \"<string>\"\n        },\n        {\n          \"field\": \"<string>\",\n          \"operator\": \"<string>\",\n          \"value\": \"<string>\"\n        }\n      ]\n    },\n    \"created_at\": \"<dateTime>\",\n    \"default\": \"<boolean>\",\n    \"id\": \"<integer>\",\n    \"position\": \"<integer>\",\n    \"raw_title\": \"<string>\",\n    \"title\": \"<string>\",\n    \"updated_at\": \"<dateTime>\"\n  }\n}"
    }
  ]
}