Chilkat Online Tools

unicodeC / ForgeRock Identity Cloud Collection / Step 4: Logout

Back to Collection Items

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

void ChilkatSample(void)
    {
    HCkHttpW http;
    BOOL success;
    HCkHttpResponseW resp;
    HCkStringBuilderW sbResponseBody;
    HCkJsonObjectW jResp;
    int respStatusCode;
    const wchar_t *result;

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

    http = CkHttpW_Create();

    CkHttpW_SetRequestHeader(http,L"{{cookieName}}",L"{{demoSSOToken}}");
    CkHttpW_SetRequestHeader(http,L"Accept-API-Version",L"resource=3.1, protocol=1.0");

    resp = CkHttpW_QuickRequest(http,L"POST",L"https://<tenant-name>.forgeblocks.com/am/json/realms/root/realms/alpha/sessions/?_action=logout");
    if (CkHttpW_getLastMethodSuccess(http) == FALSE) {
        wprintf(L"%s\n",CkHttpW_lastErrorText(http));
        CkHttpW_Dispose(http);
        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);
        CkStringBuilderW_Dispose(sbResponseBody);
        CkJsonObjectW_Dispose(jResp);
        return;
    }

    CkHttpResponseW_Dispose(resp);

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

    // {
    //   "result": "Successfully logged out"
    // }

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

    result = CkJsonObjectW_stringOf(jResp,L"result");


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

    }

Curl Command

curl -X POST
	-H "{{cookieName}}: {{demoSSOToken}}"
	-H "Accept-API-Version: resource=3.1, protocol=1.0"
https://<tenant-name>.forgeblocks.com/am/json/realms/root/realms/alpha/sessions/?_action=logout

Postman Collection Item JSON

{
  "name": "Step 4: Logout",
  "event": [
    {
      "listen": "test",
      "script": {
        "exec": [
          "// Stop running the collection",
          "postman.setNextRequest(null);",
          "pm.globals.unset(\"authId\");",
          "pm.globals.unset(\"demoSSOToken\");",
          "",
          "",
          "// Tests",
          "",
          "pm.test(\"Status code is 200.\", () => {",
          "  pm.expect(pm.response.code).to.eql(200);",
          "});",
          "",
          "pm.test(\"Response contains expected JSON.\", function () {",
          "    pm.expect(pm.response.to.have.jsonBody(\"result\",\"Successfully logged out\"));",
          "});"
        ],
        "type": "text/javascript"
      }
    }
  ],
  "protocolProfileBehavior": {
    "disableCookies": true
  },
  "request": {
    "method": "POST",
    "header": [
      {
        "description": "Specifies the session token to log out. To terminate a different session, specify its session token in the `tokenId` parameter of the POST body.",
        "key": "{{cookieName}}",
        "value": "{{demoSSOToken}}"
      },
      {
        "description": "Specifies the version of the /json/sessions endpoint to use.",
        "key": "Accept-API-Version",
        "value": "resource=3.1, protocol=1.0"
      }
    ],
    "url": {
      "raw": "{{amUrl}}/json{{realm}}/sessions/?_action=logout",
      "host": [
        "{{amUrl}}"
      ],
      "path": [
        "json{{realm}}",
        "sessions",
        ""
      ],
      "query": [
        {
          "key": "_action",
          "value": "logout",
          "description": "Specifies a request to log out, or invalidate, a session."
        }
      ]
    },
    "description": "To invalidate, or log out a session, perform an HTTP POST to the `/json/sessions/` endpoint using the `logout` action. \n\nThe endpoint invalidates the session identified by the provided session token.\n\n> Tip:\n>\n> To invalidate a different session token, include it in the POST body as the value of the `tokenId` parameter."
  },
  "response": [
    {
      "name": "Success",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "iplanetDirectoryPro",
            "value": "{{demoSSOToken}}",
            "description": "Specifies the session token to log out. To obtain information about a different token, specify it in the `tokenId` parameter of the POST body."
          },
          {
            "key": "Accept-API-Version",
            "value": "resource=3.1, protocol=1.0",
            "description": "Specifies the version of the /json/sessions endpoint to use."
          }
        ],
        "url": {
          "raw": "{{amUrl}}/json{{realm}}/sessions/?_action=logout",
          "host": [
            "{{amUrl}}"
          ],
          "path": [
            "json{{realm}}",
            "sessions",
            ""
          ],
          "query": [
            {
              "key": "_action",
              "value": "logout",
              "description": "Specifies a request to log out, or invalidate, a session."
            }
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "X-Frame-Options",
          "value": "SAMEORIGIN"
        },
        {
          "key": "X-Content-Type-Options",
          "value": "nosniff"
        },
        {
          "key": "X-Content-Type-Options",
          "value": "nosniff"
        },
        {
          "key": "Cache-Control",
          "value": "private"
        },
        {
          "key": "Cache-Control",
          "value": "no-cache, no-store, must-revalidate"
        },
        {
          "key": "Content-API-Version",
          "value": "resource=3.2"
        },
        {
          "key": "Expires",
          "value": "0"
        },
        {
          "key": "Pragma",
          "value": "no-cache"
        },
        {
          "key": "Set-Cookie",
          "value": "iPlanetDirectoryPro=\"\"; Expires=Thu, 01 Jan 1970 00:00:00 GMT; Path=/; Domain=example.com; HttpOnly"
        },
        {
          "key": "Set-Cookie",
          "value": "amlbcookie=LOGOUT; Expires=Thu, 01 Jan 1970 00:00:10 GMT; Path=/; Domain=example.com"
        },
        {
          "key": "Content-Type",
          "value": "application/json;charset=UTF-8"
        },
        {
          "key": "Content-Length",
          "value": "36"
        },
        {
          "key": "Date",
          "value": "Wed, 12 Aug 2020 14:23:45 GMT"
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"result\": \"Successfully logged out\"\n}"
    }
  ]
}