Chilkat Online Tools

unicodeC / Datadog API Collection / Post an event

Back to Collection Items

#include <C_CkHttpW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkHttpResponseW.h>
#include <C_CkStringBuilderW.h>

void ChilkatSample(void)
    {
    HCkHttpW http;
    BOOL success;
    HCkJsonObjectW json;
    HCkHttpResponseW resp;
    HCkStringBuilderW sbResponseBody;
    HCkJsonObjectW jResp;
    int respStatusCode;
    const wchar_t *strVal;
    const wchar_t *Alert_type;
    int Date_happened;
    const wchar_t *Device_name;
    const wchar_t *Host;
    int Id;
    const wchar_t *Id_str;
    const wchar_t *Payload;
    const wchar_t *Priority;
    const wchar_t *Source_type_name;
    const wchar_t *Text;
    const wchar_t *Title;
    const wchar_t *v_Url;
    const wchar_t *status;
    int i;
    int count_i;

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

    http = CkHttpW_Create();

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

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

    // {
    //   "title": "Did you hear the news today?",
    //   "text": "Oh boy!",
    //   "aggregation_key": "officia ex veniam",
    //   "alert_type": "info",
    //   "date_happened": 73027040,
    //   "device_name": "ut eu fugiat cul",
    //   "host": "i",
    //   "priority": "normal",
    //   "related_event_id": -43026196,
    //   "source_type_name": "Lorem Ut",
    //   "tags": [
    //     "environment:test"
    //   ]
    // }

    json = CkJsonObjectW_Create();
    CkJsonObjectW_UpdateString(json,L"title",L"Did you hear the news today?");
    CkJsonObjectW_UpdateString(json,L"text",L"Oh boy!");
    CkJsonObjectW_UpdateString(json,L"aggregation_key",L"officia ex veniam");
    CkJsonObjectW_UpdateString(json,L"alert_type",L"info");
    CkJsonObjectW_UpdateInt(json,L"date_happened",73027040);
    CkJsonObjectW_UpdateString(json,L"device_name",L"ut eu fugiat cul");
    CkJsonObjectW_UpdateString(json,L"host",L"i");
    CkJsonObjectW_UpdateString(json,L"priority",L"normal");
    CkJsonObjectW_UpdateInt(json,L"related_event_id",-43026196);
    CkJsonObjectW_UpdateString(json,L"source_type_name",L"Lorem Ut");
    CkJsonObjectW_UpdateString(json,L"tags[0]",L"environment:test");

    CkHttpW_SetRequestHeader(http,L"Content-Type",L"application/json");
    CkHttpW_SetRequestHeader(http,L"Accept",L"application/json");

    resp = CkHttpW_PostJson3(http,L"https://api.app.ddog-gov.com/api/v1/events",L"application/json",json);
    if (CkHttpW_getLastMethodSuccess(http) == FALSE) {
        wprintf(L"%s\n",CkHttpW_lastErrorText(http));
        CkHttpW_Dispose(http);
        CkJsonObjectW_Dispose(json);
        return;
    }

    sbResponseBody = CkStringBuilderW_Create();
    CkHttpResponseW_GetBodySb(resp,sbResponseBody);

    jResp = CkJsonObjectW_Create();
    CkJsonObjectW_LoadSb(jResp,sbResponseBody);
    CkJsonObjectW_putEmitCompact(jResp,FALSE);

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

    respStatusCode = CkHttpResponseW_getStatusCode(resp);
    wprintf(L"Response Status Code = %d\n",respStatusCode);
    if (respStatusCode >= 400) {
        wprintf(L"Response Header:\n");
        wprintf(L"%s\n",CkHttpResponseW_header(resp));
        wprintf(L"Failed.\n");
        CkHttpResponseW_Dispose(resp);
        CkHttpW_Dispose(http);
        CkJsonObjectW_Dispose(json);
        CkStringBuilderW_Dispose(sbResponseBody);
        CkJsonObjectW_Dispose(jResp);
        return;
    }

    CkHttpResponseW_Dispose(resp);

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

    // {
    //   "event": {
    //     "alert_type": "info",
    //     "date_happened": -7687934,
    //     "device_name": "labore aliquip dolor",
    //     "host": "incididunt sunt",
    //     "id": -98412440,
    //     "id_str": "incididunt laborum",
    //     "payload": "{}",
    //     "priority": "normal",
    //     "source_type_name": "commodo cupidatat ea",
    //     "tags": [
    //       "environment:test"
    //     ],
    //     "text": "Oh boy!",
    //     "title": "Did you hear the news today?",
    //     "url": "non"
    //   },
    //   "status": "sit in"
    // }

    // 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.

    Alert_type = CkJsonObjectW_stringOf(jResp,L"event.alert_type");
    Date_happened = CkJsonObjectW_IntOf(jResp,L"event.date_happened");
    Device_name = CkJsonObjectW_stringOf(jResp,L"event.device_name");
    Host = CkJsonObjectW_stringOf(jResp,L"event.host");
    Id = CkJsonObjectW_IntOf(jResp,L"event.id");
    Id_str = CkJsonObjectW_stringOf(jResp,L"event.id_str");
    Payload = CkJsonObjectW_stringOf(jResp,L"event.payload");
    Priority = CkJsonObjectW_stringOf(jResp,L"event.priority");
    Source_type_name = CkJsonObjectW_stringOf(jResp,L"event.source_type_name");
    Text = CkJsonObjectW_stringOf(jResp,L"event.text");
    Title = CkJsonObjectW_stringOf(jResp,L"event.title");
    v_Url = CkJsonObjectW_stringOf(jResp,L"event.url");
    status = CkJsonObjectW_stringOf(jResp,L"status");
    i = 0;
    count_i = CkJsonObjectW_SizeOfArray(jResp,L"event.tags");
    while (i < count_i) {
        CkJsonObjectW_putI(jResp,i);
        strVal = CkJsonObjectW_stringOf(jResp,L"event.tags[i]");
        i = i + 1;
    }



    CkHttpW_Dispose(http);
    CkJsonObjectW_Dispose(json);
    CkStringBuilderW_Dispose(sbResponseBody);
    CkJsonObjectW_Dispose(jResp);

    }

Curl Command

curl -X POST
	-H "Content-Type: application/json"
	-H "Accept: application/json"
	-d '{
  "title": "Did you hear the news today?",
  "text": "Oh boy!",
  "aggregation_key": "officia ex veniam",
  "alert_type": "info",
  "date_happened": 73027040,
  "device_name": "ut eu fugiat cul",
  "host": "i",
  "priority": "normal",
  "related_event_id": -43026196,
  "source_type_name": "Lorem Ut",
  "tags": [
    "environment:test"
  ]
}'
https://api.app.ddog-gov.com/api/v1/events

Postman Collection Item JSON

{
  "name": "Post an event",
  "request": {
    "method": "POST",
    "header": [
      {
        "key": "Content-Type",
        "value": "application/json"
      },
      {
        "key": "Accept",
        "value": "application/json"
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n  \"title\": \"Did you hear the news today?\",\n  \"text\": \"Oh boy!\",\n  \"aggregation_key\": \"officia ex veniam\",\n  \"alert_type\": \"info\",\n  \"date_happened\": 73027040,\n  \"device_name\": \"ut eu fugiat cul\",\n  \"host\": \"i\",\n  \"priority\": \"normal\",\n  \"related_event_id\": -43026196,\n  \"source_type_name\": \"Lorem Ut\",\n  \"tags\": [\n    \"environment:test\"\n  ]\n}",
      "options": {
        "raw": {
          "headerFamily": "json",
          "language": "json"
        }
      }
    },
    "url": {
      "raw": "{{baseUrl}}/api/v1/events",
      "host": [
        "{{baseUrl}}"
      ],
      "path": [
        "api",
        "v1",
        "events"
      ]
    },
    "description": "This endpoint allows you to post events to the stream.\nTag them, set priority and event aggregate them with other events."
  },
  "response": [
    {
      "name": "OK",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "value": "application/json"
          },
          {
            "key": "Accept",
            "value": "application/json"
          },
          {
            "description": "Added as a part of security scheme: apikey",
            "key": "DD-API-KEY",
            "value": "<API Key>"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n  \"title\": \"Did you hear the news today?\",\n  \"text\": \"Oh boy!\",\n  \"aggregation_key\": \"officia ex veniam\",\n  \"alert_type\": \"info\",\n  \"date_happened\": 73027040,\n  \"device_name\": \"ut eu fugiat cul\",\n  \"host\": \"i\",\n  \"priority\": \"normal\",\n  \"related_event_id\": -43026196,\n  \"source_type_name\": \"Lorem Ut\",\n  \"tags\": [\n    \"environment:test\"\n  ]\n}",
          "options": {
            "raw": {
              "headerFamily": "json",
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "{{baseUrl}}/api/v1/events",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "api",
            "v1",
            "events"
          ]
        }
      },
      "status": "Accepted",
      "code": 202,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n  \"event\": {\n    \"alert_type\": \"info\",\n    \"date_happened\": -7687934,\n    \"device_name\": \"labore aliquip dolor\",\n    \"host\": \"incididunt sunt\",\n    \"id\": -98412440,\n    \"id_str\": \"incididunt laborum\",\n    \"payload\": \"{}\",\n    \"priority\": \"normal\",\n    \"source_type_name\": \"commodo cupidatat ea\",\n    \"tags\": [\n      \"environment:test\"\n    ],\n    \"text\": \"Oh boy!\",\n    \"title\": \"Did you hear the news today?\",\n    \"url\": \"non\"\n  },\n  \"status\": \"sit in\"\n}"
    },
    {
      "name": "Bad Request",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "value": "application/json"
          },
          {
            "key": "Accept",
            "value": "application/json"
          },
          {
            "description": "Added as a part of security scheme: apikey",
            "key": "DD-API-KEY",
            "value": "<API Key>"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n  \"title\": \"Did you hear the news today?\",\n  \"text\": \"Oh boy!\",\n  \"aggregation_key\": \"officia ex veniam\",\n  \"alert_type\": \"info\",\n  \"date_happened\": 73027040,\n  \"device_name\": \"ut eu fugiat cul\",\n  \"host\": \"i\",\n  \"priority\": \"normal\",\n  \"related_event_id\": -43026196,\n  \"source_type_name\": \"Lorem Ut\",\n  \"tags\": [\n    \"environment:test\"\n  ]\n}",
          "options": {
            "raw": {
              "headerFamily": "json",
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "{{baseUrl}}/api/v1/events",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "api",
            "v1",
            "events"
          ]
        }
      },
      "status": "Bad Request",
      "code": 400,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n  \"errors\": [\n    \"Bad Request\",\n    \"Bad Request\"\n  ]\n}"
    },
    {
      "name": "Too many requests",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "value": "application/json"
          },
          {
            "key": "Accept",
            "value": "application/json"
          },
          {
            "description": "Added as a part of security scheme: apikey",
            "key": "DD-API-KEY",
            "value": "<API Key>"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n  \"title\": \"Did you hear the news today?\",\n  \"text\": \"Oh boy!\",\n  \"aggregation_key\": \"officia ex veniam\",\n  \"alert_type\": \"info\",\n  \"date_happened\": 73027040,\n  \"device_name\": \"ut eu fugiat cul\",\n  \"host\": \"i\",\n  \"priority\": \"normal\",\n  \"related_event_id\": -43026196,\n  \"source_type_name\": \"Lorem Ut\",\n  \"tags\": [\n    \"environment:test\"\n  ]\n}",
          "options": {
            "raw": {
              "headerFamily": "json",
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "{{baseUrl}}/api/v1/events",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "api",
            "v1",
            "events"
          ]
        }
      },
      "status": "Too Many Requests",
      "code": 429,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n  \"errors\": [\n    \"Bad Request\",\n    \"Bad Request\"\n  ]\n}"
    }
  ]
}