Chilkat Online Tools

C / Postman API / All Environments

Back to Collection Items

#include <C_CkHttp.h>
#include <C_CkStringBuilder.h>
#include <C_CkJsonObject.h>

void ChilkatSample(void)
    {
    HCkHttp http;
    BOOL success;
    HCkStringBuilder sbResponseBody;
    HCkJsonObject jResp;
    int respStatusCode;
    const char *id;
    const char *name;
    const char *createdAt;
    const char *updatedAt;
    const char *owner;
    const char *uid;
    BOOL isPublic;
    int i;
    int count_i;

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

    http = CkHttp_Create();

    CkHttp_SetRequestHeader(http,"X-API-Key","{{postman_api_key}}");

    sbResponseBody = CkStringBuilder_Create();
    success = CkHttp_QuickGetSb(http,"https://api.getpostman.com/environments",sbResponseBody);
    if (success == FALSE) {
        printf("%s\n",CkHttp_lastErrorText(http));
        CkHttp_Dispose(http);
        CkStringBuilder_Dispose(sbResponseBody);
        return;
    }

    jResp = CkJsonObject_Create();
    CkJsonObject_LoadSb(jResp,sbResponseBody);
    CkJsonObject_putEmitCompact(jResp,FALSE);

    printf("Response Body:\n");
    printf("%s\n",CkJsonObject_emit(jResp));

    respStatusCode = CkHttp_getLastStatus(http);
    printf("Response Status Code = %d\n",respStatusCode);
    if (respStatusCode >= 400) {
        printf("Response Header:\n");
        printf("%s\n",CkHttp_lastHeader(http));
        printf("Failed.\n");
        CkHttp_Dispose(http);
        CkStringBuilder_Dispose(sbResponseBody);
        CkJsonObject_Dispose(jResp);
        return;
    }

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

    // {
    //   "environments": [
    //     {
    //       "id": "5daabc50-8451-43f6-922d-96b403b4f28e",
    //       "name": "Users API (prod)",
    //       "createdAt": "2020-09-23T14:31:18.000Z",
    //       "updatedAt": "2020-12-04T14:13:40.000Z",
    //       "owner": "10016724",
    //       "uid": "10016724-5daabc50-8451-43f6-922d-96b403b4f28e",
    //       "isPublic": false
    //     },
    //     {
    //       "id": "7d786cc8-142b-4d62-b5a5-872afc37ad16",
    //       "name": "Environment Scanner",
    //       "createdAt": "2020-02-04T19:34:23.000Z",
    //       "updatedAt": "2020-08-12T13:34:06.000Z",
    //       "owner": "10016724",
    //       "uid": "10016724-7d786cc8-142b-4d62-b5a5-872afc37ad16",
    //       "isPublic": false
    //     }
    //   ]
    // }

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

    i = 0;
    count_i = CkJsonObject_SizeOfArray(jResp,"environments");
    while (i < count_i) {
        CkJsonObject_putI(jResp,i);
        id = CkJsonObject_stringOf(jResp,"environments[i].id");
        name = CkJsonObject_stringOf(jResp,"environments[i].name");
        createdAt = CkJsonObject_stringOf(jResp,"environments[i].createdAt");
        updatedAt = CkJsonObject_stringOf(jResp,"environments[i].updatedAt");
        owner = CkJsonObject_stringOf(jResp,"environments[i].owner");
        uid = CkJsonObject_stringOf(jResp,"environments[i].uid");
        isPublic = CkJsonObject_BoolOf(jResp,"environments[i].isPublic");
        i = i + 1;
    }



    CkHttp_Dispose(http);
    CkStringBuilder_Dispose(sbResponseBody);
    CkJsonObject_Dispose(jResp);

    }

Curl Command

curl -X GET
	-H "X-API-Key: {{postman_api_key}}"
https://api.getpostman.com/environments

Postman Collection Item JSON

{
  "name": "All Environments",
  "event": [
    {
      "listen": "test",
      "script": {
        "type": "text/javascript",
        "exec": [
          "var responseJSON;",
          "",
          "// parse response body as JSON",
          "try { responseJSON = JSON.parse(responseBody); } catch (e) { }",
          "tests['received a valid response JSON'] = !!responseJSON;",
          "!responseJSON && (responseJSON = {});",
          "",
          "tests['status code is 200'] = (responseCode.code === 200);",
          "tests['response does not have errors'] = (!responseJSON.error);",
          "",
          "tests['response has an array of environments'] = Array.isArray(responseJSON.environments);"
        ]
      }
    }
  ],
  "request": {
    "method": "GET",
    "header": [
    ],
    "url": {
      "raw": "https://api.getpostman.com/environments",
      "protocol": "https",
      "host": [
        "api",
        "getpostman",
        "com"
      ],
      "path": [
        "environments"
      ]
    },
    "description": "The `/environments` endpoint returns a list of all [environments](https://www.getpostman.com/docs/environments) that belong to you..\n\nThe response contains an array of environments' information containing the `name`, `id`, `owner` and `uid` of each environment.\n\n> Requires <a href=\"#authentication\">API Key</a> as `X-Api-Key` request header or `apikey` URL query parameter."
  },
  "response": [
    {
      "name": "Valid Response",
      "originalRequest": {
        "method": "GET",
        "header": [
        ],
        "url": {
          "raw": "https://api.getpostman.com/environments",
          "protocol": "https",
          "host": [
            "api",
            "getpostman",
            "com"
          ],
          "path": [
            "environments"
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json",
          "description": {
            "content": "",
            "type": "text/plain"
          }
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"environments\": [\n       {\n            \"id\": \"5daabc50-8451-43f6-922d-96b403b4f28e\",\n            \"name\": \"Users API (prod)\",\n            \"createdAt\": \"2020-09-23T14:31:18.000Z\",\n            \"updatedAt\": \"2020-12-04T14:13:40.000Z\",\n            \"owner\": \"10016724\",\n            \"uid\": \"10016724-5daabc50-8451-43f6-922d-96b403b4f28e\",\n            \"isPublic\": false\n        },\n        {\n            \"id\": \"7d786cc8-142b-4d62-b5a5-872afc37ad16\",\n            \"name\": \"Environment Scanner\",\n            \"createdAt\": \"2020-02-04T19:34:23.000Z\",\n            \"updatedAt\": \"2020-08-12T13:34:06.000Z\",\n            \"owner\": \"10016724\",\n            \"uid\": \"10016724-7d786cc8-142b-4d62-b5a5-872afc37ad16\",\n            \"isPublic\": false\n        }\n    ]\n}"
    }
  ]
}