unicodeC / Postman API / Single Environment
Back to Collection Items
#include <C_CkHttpW.h>
#include <C_CkStringBuilderW.h>
#include <C_CkJsonObjectW.h>
void ChilkatSample(void)
{
HCkHttpW http;
BOOL success;
HCkStringBuilderW sbResponseBody;
HCkJsonObjectW jResp;
int respStatusCode;
const wchar_t *key;
const wchar_t *value;
BOOL enabled;
const wchar_t *Id;
const wchar_t *Name;
const wchar_t *Owner;
const wchar_t *CreatedAt;
const wchar_t *UpdatedAt;
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 = CkHttpW_Create();
CkHttpW_SetRequestHeader(http,L"X-API-Key",L"{{postman_api_key}}");
sbResponseBody = CkStringBuilderW_Create();
success = CkHttpW_QuickGetSb(http,L"https://api.getpostman.com/environments/{{environment_uid}}",sbResponseBody);
if (success == FALSE) {
wprintf(L"%s\n",CkHttpW_lastErrorText(http));
CkHttpW_Dispose(http);
CkStringBuilderW_Dispose(sbResponseBody);
return;
}
jResp = CkJsonObjectW_Create();
CkJsonObjectW_LoadSb(jResp,sbResponseBody);
CkJsonObjectW_putEmitCompact(jResp,FALSE);
wprintf(L"Response Body:\n");
wprintf(L"%s\n",CkJsonObjectW_emit(jResp));
respStatusCode = CkHttpW_getLastStatus(http);
wprintf(L"Response Status Code = %d\n",respStatusCode);
if (respStatusCode >= 400) {
wprintf(L"Response Header:\n");
wprintf(L"%s\n",CkHttpW_lastHeader(http));
wprintf(L"Failed.\n");
CkHttpW_Dispose(http);
CkStringBuilderW_Dispose(sbResponseBody);
CkJsonObjectW_Dispose(jResp);
return;
}
// Sample JSON response:
// (Sample code for parsing the JSON response is shown below)
// {
// "environment": {
// "id": "1b163ddc-62ff-41d9-b74b-71424b9aa69d",
// "name": "Contract Test Environment",
// "owner": "10016724",
// "createdAt": "2020-11-05T13:59:22.000Z",
// "updatedAt": "2020-11-05T13:59:23.000Z",
// "values": [
// {
// "key": "env-apiKey",
// "value": "",
// "enabled": true
// }
// ],
// "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.
Id = CkJsonObjectW_stringOf(jResp,L"environment.id");
Name = CkJsonObjectW_stringOf(jResp,L"environment.name");
Owner = CkJsonObjectW_stringOf(jResp,L"environment.owner");
CreatedAt = CkJsonObjectW_stringOf(jResp,L"environment.createdAt");
UpdatedAt = CkJsonObjectW_stringOf(jResp,L"environment.updatedAt");
IsPublic = CkJsonObjectW_BoolOf(jResp,L"environment.isPublic");
i = 0;
count_i = CkJsonObjectW_SizeOfArray(jResp,L"environment.values");
while (i < count_i) {
CkJsonObjectW_putI(jResp,i);
key = CkJsonObjectW_stringOf(jResp,L"environment.values[i].key");
value = CkJsonObjectW_stringOf(jResp,L"environment.values[i].value");
enabled = CkJsonObjectW_BoolOf(jResp,L"environment.values[i].enabled");
i = i + 1;
}
CkHttpW_Dispose(http);
CkStringBuilderW_Dispose(sbResponseBody);
CkJsonObjectW_Dispose(jResp);
}
Curl Command
curl -X GET
-H "X-API-Key: {{postman_api_key}}"
https://api.getpostman.com/environments/{{environment_uid}}
Postman Collection Item JSON
{
"name": "Single Environment",
"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 environment object'] = (typeof responseJSON.environment === 'object');"
]
}
}
],
"request": {
"method": "GET",
"header": [
],
"url": {
"raw": "https://api.getpostman.com/environments/{{environment_uid}}",
"protocol": "https",
"host": [
"api",
"getpostman",
"com"
],
"path": [
"environments",
"{{environment_uid}}"
]
},
"description": "Access the contents of an environment that is accessible to you using its unique id (`uid`).\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/{{environment_uid}}",
"protocol": "https",
"host": [
"api",
"getpostman",
"com"
],
"path": [
"environments",
"{{environment_uid}}"
]
}
},
"status": "OK",
"code": 200,
"_postman_previewlanguage": "json",
"header": [
{
"key": "Content-Type",
"value": "application/json",
"name": "Content-Type",
"description": {
"content": "",
"type": "text/plain"
}
}
],
"cookie": [
],
"body": "{\n \"environment\": {\n \"id\": \"1b163ddc-62ff-41d9-b74b-71424b9aa69d\",\n \"name\": \"Contract Test Environment\",\n \"owner\": \"10016724\",\n \"createdAt\": \"2020-11-05T13:59:22.000Z\",\n \"updatedAt\": \"2020-11-05T13:59:23.000Z\",\n \"values\": [\n {\n \"key\": \"env-apiKey\",\n \"value\": \"\",\n \"enabled\": true\n }\n ],\n \"isPublic\": false\n }\n}"
}
]
}