Chilkat Online Tools

unicodeC / ForgeRock Identity Cloud Collection / Step 3: Tail the Audit Logs for a Source

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 queryParams;
    HCkHttpResponseW resp;
    HCkStringBuilderW sbResponseBody;
    HCkJsonObjectW jResp;
    int respStatusCode;
    const wchar_t *payload;
    const wchar_t *timestamp;
    const wchar_t *v_type;
    const wchar_t *resultCount;
    const wchar_t *pagedResultsCookie;
    const wchar_t *totalPagedResultsPolicy;
    const wchar_t *totalPagedResults;
    const wchar_t *remainingPagedResults;
    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();

    queryParams = CkJsonObjectW_Create();
    CkJsonObjectW_UpdateString(queryParams,L"source",L"{{auditSource}}");
    CkJsonObjectW_UpdateString(queryParams,L"_prettyPrint",L"true");

    CkHttpW_SetRequestHeader(http,L"x-api-secret",L"{{logApiSecret}}");
    CkHttpW_SetRequestHeader(http,L"x-api-key",L"{{logApiKey}}");

    resp = CkHttpW_QuickRequestParams(http,L"GET",L"https://<tenant-name>.forgeblocks.com/monitoring/logs/tail",queryParams);
    if (CkHttpW_getLastMethodSuccess(http) == FALSE) {
        wprintf(L"%s\n",CkHttpW_lastErrorText(http));
        CkHttpW_Dispose(http);
        CkJsonObjectW_Dispose(queryParams);
        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(queryParams);
        CkStringBuilderW_Dispose(sbResponseBody);
        CkJsonObjectW_Dispose(jResp);
        return;
    }

    CkHttpResponseW_Dispose(resp);

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

    // {
    //   "result": [
    //     {
    //       "payload": "<object>",
    //       "timestamp": "<dateTime>",
    //       "type": "<string>"
    //     },
    //     {
    //       "payload": "<object>",
    //       "timestamp": "<dateTime>",
    //       "type": "<string>"
    //     }
    //   ],
    //   "resultCount": "<integer>",
    //   "pagedResultsCookie": "<string>",
    //   "totalPagedResultsPolicy": "<string>",
    //   "totalPagedResults": "<integer>",
    //   "remainingPagedResults": "<integer>"
    // }

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

    resultCount = CkJsonObjectW_stringOf(jResp,L"resultCount");
    pagedResultsCookie = CkJsonObjectW_stringOf(jResp,L"pagedResultsCookie");
    totalPagedResultsPolicy = CkJsonObjectW_stringOf(jResp,L"totalPagedResultsPolicy");
    totalPagedResults = CkJsonObjectW_stringOf(jResp,L"totalPagedResults");
    remainingPagedResults = CkJsonObjectW_stringOf(jResp,L"remainingPagedResults");
    i = 0;
    count_i = CkJsonObjectW_SizeOfArray(jResp,L"result");
    while (i < count_i) {
        CkJsonObjectW_putI(jResp,i);
        payload = CkJsonObjectW_stringOf(jResp,L"result[i].payload");
        timestamp = CkJsonObjectW_stringOf(jResp,L"result[i].timestamp");
        v_type = CkJsonObjectW_stringOf(jResp,L"result[i].type");
        i = i + 1;
    }



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

    }

Curl Command

curl -G -d "source=%7B%7BauditSource%7D%7D"
	-d "_prettyPrint=true"
	-H "x-api-key: {{logApiKey}}"
	-H "x-api-secret: {{logApiSecret}}"
https://<tenant-name>.forgeblocks.com/monitoring/logs/tail

Postman Collection Item JSON

{
  "name": "Step 3: Tail the Audit Logs for a Source",
  "event": [
    {
      "listen": "test",
      "script": {
        "exec": [
          "const jsonData = JSON.parse(responseBody);",
          "if(jsonData.pagedResultsCookie && jsonData.pagedResultsCookie != \"\"){",
          "    pm.globals.set(\"pagedResultsCookie\", jsonData.pagedResultsCookie);",
          "}",
          "",
          "// Tests",
          "",
          "pm.test(\"Status code is 200.\", () => {",
          "  pm.expect(pm.response.code).to.eql(200);",
          "});",
          "",
          "pm.test(\"Response contains results\", function () {",
          "    pm.expect(jsonData).to.have.any.keys('resultCount', 'result');",
          "});",
          "",
          "pm.test(\"Response contains pagedResultsCookie\", function () {",
          "    pm.expect(jsonData).to.have.any.keys('pagedResultsCookie');",
          "});"
        ],
        "type": "text/javascript"
      }
    }
  ],
  "request": {
    "auth": {
      "type": "noauth"
    },
    "method": "GET",
    "header": [
      {
        "key": "x-api-key",
        "value": "{{logApiKey}}",
        "type": "text"
      },
      {
        "key": "x-api-secret",
        "value": "{{logApiSecret}}",
        "type": "text"
      }
    ],
    "url": {
      "raw": "{{platformUrl}}/monitoring/logs/tail?source={{auditSource}}&_prettyPrint=true",
      "host": [
        "{{platformUrl}}"
      ],
      "path": [
        "monitoring",
        "logs",
        "tail"
      ],
      "query": [
        {
          "key": "source",
          "value": "{{auditSource}}"
        },
        {
          "key": "_prettyPrint",
          "value": "true"
        }
      ]
    },
    "description": "Tail log events"
  },
  "response": [
    {
      "name": "List of log events",
      "originalRequest": {
        "method": "GET",
        "header": [
        ],
        "url": {
          "raw": "{{baseUrl}}/logs/tail?source=<string>&_pagedResultsCookie=<string>&_prettyPrint=false",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "logs",
            "tail"
          ],
          "query": [
            {
              "key": "source",
              "value": "<string>"
            },
            {
              "key": "_pagedResultsCookie",
              "value": "<string>"
            },
            {
              "key": "_prettyPrint",
              "value": "false"
            }
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n\t\"result\": [\n\t\t{\n\t\t\t\"payload\": \"<object>\",\n\t\t\t\"timestamp\": \"<dateTime>\",\n\t\t\t\"type\": \"<string>\"\n\t\t},\n\t\t{\n\t\t\t\"payload\": \"<object>\",\n\t\t\t\"timestamp\": \"<dateTime>\",\n\t\t\t\"type\": \"<string>\"\n\t\t}\n\t],\n\t\"resultCount\": \"<integer>\",\n\t\"pagedResultsCookie\": \"<string>\",\n\t\"totalPagedResultsPolicy\": \"<string>\",\n\t\"totalPagedResults\": \"<integer>\",\n\t\"remainingPagedResults\": \"<integer>\"\n}"
    }
  ]
}