Chilkat Online Tools

Java / Datadog API Collection / Post an event

Back to Collection Items

import com.chilkatsoft.*;

public class ChilkatExample {

  static {
    try {
        System.loadLibrary("chilkat");
    } catch (UnsatisfiedLinkError e) {
      System.err.println("Native code library failed to load.\n" + e);
      System.exit(1);
    }
  }

  public static void main(String argv[])
  {
    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    CkHttp http = new CkHttp();
    boolean 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.

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

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

    http.SetRequestHeader("Content-Type","application/json");
    http.SetRequestHeader("Accept","application/json");

    CkHttpResponse resp = http.PostJson3("https://api.app.ddog-gov.com/api/v1/events","application/json",json);
    if (http.get_LastMethodSuccess() == false) {
        System.out.println(http.lastErrorText());
        return;
        }

    CkStringBuilder sbResponseBody = new CkStringBuilder();
    resp.GetBodySb(sbResponseBody);

    CkJsonObject jResp = new CkJsonObject();
    jResp.LoadSb(sbResponseBody);
    jResp.put_EmitCompact(false);

    System.out.println("Response Body:");
    System.out.println(jResp.emit());

    int respStatusCode = resp.get_StatusCode();
    System.out.println("Response Status Code = " + respStatusCode);
    if (respStatusCode >= 400) {
        System.out.println("Response Header:");
        System.out.println(resp.header());
        System.out.println("Failed.");

        return;
        }

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

    String strVal;

    String Alert_type = jResp.stringOf("event.alert_type");
    int Date_happened = jResp.IntOf("event.date_happened");
    String Device_name = jResp.stringOf("event.device_name");
    String Host = jResp.stringOf("event.host");
    int Id = jResp.IntOf("event.id");
    String Id_str = jResp.stringOf("event.id_str");
    String Payload = jResp.stringOf("event.payload");
    String Priority = jResp.stringOf("event.priority");
    String Source_type_name = jResp.stringOf("event.source_type_name");
    String Text = jResp.stringOf("event.text");
    String Title = jResp.stringOf("event.title");
    String v_Url = jResp.stringOf("event.url");
    String status = jResp.stringOf("status");
    int i = 0;
    int count_i = jResp.SizeOfArray("event.tags");
    while (i < count_i) {
        jResp.put_I(i);
        strVal = jResp.stringOf("event.tags[i]");
        i = i+1;
        }
  }
}

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