Chilkat Online Tools

TCL / ForgeRock Identity Cloud Collection / Step 4: Tail the Audit Logs for a Source Since Last Request

Back to Collection Items

load ./chilkat.dll

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

set http [new_CkHttp]

set queryParams [new_CkJsonObject]

CkJsonObject_UpdateString $queryParams "source" "{{auditSource}}"
CkJsonObject_UpdateString $queryParams "_pagedResultsCookie" "{{pagedResultsCookie}}"
CkJsonObject_UpdateString $queryParams "_prettyPrint" "false"

CkHttp_SetRequestHeader $http "x-api-secret" "{{logApiSecret}}"
CkHttp_SetRequestHeader $http "x-api-key" "{{logApiKey}}"

# resp is a CkHttpResponse
set resp [CkHttp_QuickRequestParams $http "GET" "https://<tenant-name>.forgeblocks.com/monitoring/logs/tail" $queryParams]
if {[CkHttp_get_LastMethodSuccess $http] == 0} then {
    puts [CkHttp_lastErrorText $http]
    delete_CkHttp $http
    delete_CkJsonObject $queryParams
    exit
}

set sbResponseBody [new_CkStringBuilder]

CkHttpResponse_GetBodySb $resp $sbResponseBody

set jResp [new_CkJsonObject]

CkJsonObject_LoadSb $jResp $sbResponseBody
CkJsonObject_put_EmitCompact $jResp 0

puts "Response Body:"
puts [CkJsonObject_emit $jResp]

set respStatusCode [CkHttpResponse_get_StatusCode $resp]
puts "Response Status Code = $respStatusCode"
if {$respStatusCode >= 400} then {
    puts "Response Header:"
    puts [CkHttpResponse_header $resp]
    puts "Failed."
    delete_CkHttpResponse $resp

    delete_CkHttp $http
    delete_CkJsonObject $queryParams
    delete_CkStringBuilder $sbResponseBody
    delete_CkJsonObject $jResp
    exit
}

delete_CkHttpResponse $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

set resultCount [CkJsonObject_stringOf $jResp "resultCount"]
set pagedResultsCookie [CkJsonObject_stringOf $jResp "pagedResultsCookie"]
set totalPagedResultsPolicy [CkJsonObject_stringOf $jResp "totalPagedResultsPolicy"]
set totalPagedResults [CkJsonObject_stringOf $jResp "totalPagedResults"]
set remainingPagedResults [CkJsonObject_stringOf $jResp "remainingPagedResults"]
set i 0
set count_i [CkJsonObject_SizeOfArray $jResp "result"]
while {$i < $count_i} {
    CkJsonObject_put_I $jResp $i
    set payload [CkJsonObject_stringOf $jResp "result[i].payload"]
    set timestamp [CkJsonObject_stringOf $jResp "result[i].timestamp"]
    set v_type [CkJsonObject_stringOf $jResp "result[i].type"]
    set i [expr $i + 1]
}

delete_CkHttp $http
delete_CkJsonObject $queryParams
delete_CkStringBuilder $sbResponseBody
delete_CkJsonObject $jResp

Curl Command

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

Postman Collection Item JSON

{
  "name": "Step 4: Tail the Audit Logs for a Source Since Last Request",
  "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",
        "type": "text",
        "value": "{{logApiKey}}"
      },
      {
        "key": "x-api-secret",
        "type": "text",
        "value": "{{logApiSecret}}"
      }
    ],
    "url": {
      "raw": "{{platformUrl}}/monitoring/logs/tail?source={{auditSource}}&_pagedResultsCookie={{pagedResultsCookie}}&_prettyPrint=false",
      "host": [
        "{{platformUrl}}"
      ],
      "path": [
        "monitoring",
        "logs",
        "tail"
      ],
      "query": [
        {
          "key": "source",
          "value": "{{auditSource}}"
        },
        {
          "key": "_pagedResultsCookie",
          "value": "{{pagedResultsCookie}}"
        },
        {
          "key": "_prettyPrint",
          "value": "false"
        }
      ]
    },
    "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}"
    }
  ]
}