Chilkat Online Tools

DataFlex / Postman API / Create Environment

Back to Collection Items

Use ChilkatAx-win32.pkg

Procedure Test
    Handle hoHttp
    Boolean iSuccess
    Variant vJson
    Handle hoJson
    Variant vResp
    Handle hoResp
    Variant vSbResponseBody
    Handle hoSbResponseBody
    Handle hoJResp
    Integer iRespStatusCode
    String sId
    String sName
    String sUid
    String sTemp1
    Boolean bTemp1

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

    Get Create (RefClass(cComChilkatHttp)) To hoHttp
    If (Not(IsComObjectCreated(hoHttp))) Begin
        Send CreateComObject of hoHttp
    End

    // Use this online tool to generate code from sample JSON: Generate Code to Create JSON

    // The following JSON is sent in the request body.

    // {
    //   "environment": {
    //     "name": "Sample Environment Name (required)",
    //     "values": [
    //       {
    //         "key": "variable_name_1",
    //         "value": "The value of the variable"
    //       },
    //       {
    //         "key": "variable_name_2",
    //         "value": ""
    //       }
    //     ]
    //   }
    // }

    Get Create (RefClass(cComChilkatJsonObject)) To hoJson
    If (Not(IsComObjectCreated(hoJson))) Begin
        Send CreateComObject of hoJson
    End
    Get ComUpdateString Of hoJson "environment.name" "Sample Environment Name (required)" To iSuccess
    Get ComUpdateString Of hoJson "environment.values[0].key" "variable_name_1" To iSuccess
    Get ComUpdateString Of hoJson "environment.values[0].value" "The value of the variable" To iSuccess
    Get ComUpdateString Of hoJson "environment.values[1].key" "variable_name_2" To iSuccess
    Get ComUpdateString Of hoJson "environment.values[1].value" "" To iSuccess

    Send ComSetRequestHeader To hoHttp "Content-Type" "application/json"
    Send ComSetRequestHeader To hoHttp "X-API-Key" "{{postman_api_key}}"

    Get pvComObject of hoJson to vJson
    Get ComPostJson3 Of hoHttp "https://api.getpostman.com/environments" "application/json" vJson To vResp
    If (IsComObject(vResp)) Begin
        Get Create (RefClass(cComChilkatHttpResponse)) To hoResp
        Set pvComObject Of hoResp To vResp
    End
    Get ComLastMethodSuccess Of hoHttp To bTemp1
    If (bTemp1 = False) Begin
        Get ComLastErrorText Of hoHttp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbResponseBody
    If (Not(IsComObjectCreated(hoSbResponseBody))) Begin
        Send CreateComObject of hoSbResponseBody
    End
    Get pvComObject of hoSbResponseBody to vSbResponseBody
    Get ComGetBodySb Of hoResp vSbResponseBody To iSuccess

    Get Create (RefClass(cComChilkatJsonObject)) To hoJResp
    If (Not(IsComObjectCreated(hoJResp))) Begin
        Send CreateComObject of hoJResp
    End
    Get pvComObject of hoSbResponseBody to vSbResponseBody
    Get ComLoadSb Of hoJResp vSbResponseBody To iSuccess
    Set ComEmitCompact Of hoJResp To False

    Showln "Response Body:"
    Get ComEmit Of hoJResp To sTemp1
    Showln sTemp1

    Get ComStatusCode Of hoResp To iRespStatusCode
    Showln "Response Status Code = " iRespStatusCode
    If (iRespStatusCode >= 400) Begin
        Showln "Response Header:"
        Get ComHeader Of hoResp To sTemp1
        Showln sTemp1
        Showln "Failed."
        Send Destroy of hoResp
        Procedure_Return
    End

    Send Destroy of hoResp

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

    // {
    //   "environment": {
    //     "id": "f158266e-306b-4702-a2b9-e4ede7878b7a",
    //     "name": "Sample Environment Name (required)",
    //     "uid": "5665-f158266e-306b-4702-a2b9-e4ede7878b7a"
    //   }
    // }

    // Sample code for parsing the JSON response...
    // Use this online tool to generate parsing code from sample JSON: Generate JSON Parsing Code

    Get ComStringOf Of hoJResp "environment.id" To sId
    Get ComStringOf Of hoJResp "environment.name" To sName
    Get ComStringOf Of hoJResp "environment.uid" To sUid


End_Procedure

Curl Command

curl -X POST
	-H "X-API-Key: {{postman_api_key}}"
	-H "Content-Type: application/json"
	-d '{
    "environment": {
        "name": "Sample Environment Name (required)",
        "values": [
            {
                "key": "variable_name_1",
                "value": "The value of the variable"
            },
            {
                "key": "variable_name_2",
                "value": ""
            }
        ]
    }
}'
https://api.getpostman.com/environments

Postman Collection Item JSON

{
  "name": "Create Environment",
  "request": {
    "method": "POST",
    "header": [
      {
        "key": "Content-Type",
        "value": "application/json"
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n    \"environment\": {\n        \"name\": \"Sample Environment Name (required)\",\n        \"values\": [\n            {\n                \"key\": \"variable_name_1\",\n                \"value\": \"The value of the variable\"\n            },\n            {\n                \"key\": \"variable_name_2\",\n                \"value\": \"\"\n            }\n        ]\n    }\n}"
    },
    "url": {
      "raw": "https://api.getpostman.com/environments",
      "protocol": "https",
      "host": [
        "api",
        "getpostman",
        "com"
      ],
      "path": [
        "environments"
      ]
    },
    "description": "A sample body is added to the request that conforms to the following JSON schema:\n\n```json\n{\n  \"type\": \"object\",\n  \"properties\": {\n    \"environment\": {\n      \"type\": \"object\",\n      \"properties\": {\n        \"name\": {\n              \"type\": \"string\",\n              \"maxLength\": 254,\n              \"minLength\": 1\n            },\n        \"values\": {\n          \"type\": \"array\",\n          \"maxItems\": 100,\n          \"additionalItems\": false,\n          \"items\": {\n              \"type\": \"object\",\n              \"properties\": {\n                \"key\": {\n                  \"type\": \"string\",\n                  \"maxLength\": 254\n                  \"minLength\": 1\n                },\n                \"value\": { \"type\": \"string\" },\n                \"enabled\": { \"type\": \"boolean\" }\n              },\n              \"required\": [\"key\", \"value\"]\n            }\n        }\n      },\n      \"required\": [\"name\"]\n    }\n  },\n  \"required\": [\"environment\"]\n}\n```\n\nOn successful creation of the environment, the API returns the environment name and `id`.\n\nYou can also specify the context of a workspace to create an environment in directly by passing the `workspace` as a query param.\n\n> Requires <a href=\"#authentication\">API Key</a> as `X-Api-Key` request header or `apikey` URL query parameter."
  },
  "response": [
    {
      "name": "Create Environment in a Workspace",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "name": "Content-Type",
            "key": "Content-Type",
            "value": "application/json"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n    \"environment\": {\n        \"name\": \"Sample Environment Name (required)\",\n        \"values\": [\n            {\n                \"key\": \"variable_name_1\",\n                \"value\": \"The value of the variable\"\n            },\n            {\n                \"key\": \"variable_name_2\",\n                \"value\": \"\"\n            }\n        ]\n    }\n}",
          "options": {
            "raw": {
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "https://api.getpostman.com/environments?workspace={{workspace_id}}",
          "protocol": "https",
          "host": [
            "api",
            "getpostman",
            "com"
          ],
          "path": [
            "environments"
          ],
          "query": [
            {
              "key": "workspace",
              "value": "{{workspace_id}}",
              "description": "The workspace ID in which the environment will be created"
            }
          ]
        }
      },
      "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\": \"f158266e-306b-4702-a2b9-e4ede7878b7a\",\n        \"name\": \"Sample Environment Name (required)\",\n        \"uid\": \"5665-f158266e-306b-4702-a2b9-e4ede7878b7a\"\n    }\n}"
    },
    {
      "name": "Successful Response",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "name": "Content-Type",
            "key": "Content-Type",
            "value": "application/json"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n    \"environment\": {\n        \"name\": \"Sample Environment Name (required)\",\n        \"values\": [\n            {\"key\": \"variable_name_1\", \"value\": \"The value of the variable\"},\n            {\"key\": \"variable_name_2\", \"value\": \"\"}\n        ]\n    }\n}",
          "options": {
            "raw": {
              "language": "json"
            }
          }
        },
        "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",
          "name": "Content-Type",
          "description": {
            "content": "",
            "type": "text/plain"
          }
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"environment\": {\n        \"id\": \"f158266e-306b-4702-a2b9-e4ede7878b7a\",\n        \"name\": \"Sample Environment Name (required)\",\n        \"uid\": \"5665-f158266e-306b-4702-a2b9-e4ede7878b7a\"\n    }\n}"
    },
    {
      "name": "Failure Response",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "value": "application/json"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n    \n}"
        },
        "url": {
          "raw": "https://api.getpostman.com/environments",
          "protocol": "https",
          "host": [
            "api",
            "getpostman",
            "com"
          ],
          "path": [
            "environments"
          ]
        }
      },
      "status": "Bad Request",
      "code": 400,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json; charset=utf-8"
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"error\": {\n        \"name\": \"malformedRequestError\",\n        \"message\": \"Found 1 errors with the supplied environment.\",\n        \"details\": [\n            \": must have required property 'environment'\"\n        ]\n    }\n}"
    }
  ]
}