Chilkat Online Tools

Classic ASP / Postman API / Create collection from schema

Back to Collection Items

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<%
' This example assumes the Chilkat API to have been previously unlocked.
' See Global Unlock Sample for sample code.

set http = Server.CreateObject("Chilkat_9_5_0.Http")

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

' The following JSON is sent in the request body.

' {
'   "name": "My generated collection",
'   "relations": [
'     {
'       "type": "contracttest"
'     }
'   ]
' }

set json = Server.CreateObject("Chilkat_9_5_0.JsonObject")
success = json.UpdateString("name","My generated collection")
success = json.UpdateString("relations[0].type","contracttest")

http.SetRequestHeader "Content-Type","application/json"
http.SetRequestHeader "X-API-Key","{{postman_api_key}}"

' resp is a Chilkat_9_5_0.HttpResponse
Set resp = http.PostJson3("https://api.getpostman.com/apis/{{apiId}}/versions/{{apiVersionId}}/schemas/{{schemaId}}/collections?workspace=c90a3482-f80d-43d6-9dfb-0830f5e43605","application/json",json)
If (http.LastMethodSuccess = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( http.LastErrorText) & "</pre>"
    Response.End
End If

set sbResponseBody = Server.CreateObject("Chilkat_9_5_0.StringBuilder")
success = resp.GetBodySb(sbResponseBody)

set jResp = Server.CreateObject("Chilkat_9_5_0.JsonObject")
success = jResp.LoadSb(sbResponseBody)
jResp.EmitCompact = 0

Response.Write "<pre>" & Server.HTMLEncode( "Response Body:") & "</pre>"
Response.Write "<pre>" & Server.HTMLEncode( jResp.Emit()) & "</pre>"

respStatusCode = resp.StatusCode
Response.Write "<pre>" & Server.HTMLEncode( "Response Status Code = " & respStatusCode) & "</pre>"
If (respStatusCode >= 400) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Response Header:") & "</pre>"
    Response.Write "<pre>" & Server.HTMLEncode( resp.Header) & "</pre>"
    Response.Write "<pre>" & Server.HTMLEncode( "Failed.") & "</pre>"

    Response.End
End If

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

' {
'   "collection": {
'     "id": "e6b0d46a-8722-4f42-ab86-f5f473187ddf",
'     "uid": "112098-e6b0d46a-8722-4f42-ab86-f5f473187ddf"
'   },
'   "relations": [
'     {
'       "type": "contracttest",
'       "id": "4b40f06a-5a6a-448f-bfcd-a6dbcb68da22"
'     }
'   ]
' }

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

Id = jResp.StringOf("collection.id")
Uid = jResp.StringOf("collection.uid")
i = 0
count_i = jResp.SizeOfArray("relations")
Do While i < count_i
    jResp.I = i
    v_type = jResp.StringOf("relations[i].type")
    id = jResp.StringOf("relations[i].id")
    i = i + 1
Loop

%>
</body>
</html>

Curl Command

curl -X POST
	-H "X-API-Key: {{postman_api_key}}"
	-H "Content-Type: application/json"
	-d '{
	"name": "My generated collection",
	"relations": [
		{
			"type": "contracttest"
		}	
	]
}'
https://api.getpostman.com/apis/{{apiId}}/versions/{{apiVersionId}}/schemas/{{schemaId}}/collections?workspace=c90a3482-f80d-43d6-9dfb-0830f5e43605

Postman Collection Item JSON

{
  "name": "Create collection from schema",
  "request": {
    "method": "POST",
    "header": [
      {
        "key": "Content-Type",
        "value": "application/json"
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n\t\"name\": \"My generated collection\",\n\t\"relations\": [\n\t\t{\n\t\t\t\"type\": \"contracttest\"\n\t\t}\t\n\t]\n}",
      "options": {
        "raw": {
          "language": "json"
        }
      }
    },
    "url": {
      "raw": "https://api.getpostman.com/apis/{{apiId}}/versions/{{apiVersionId}}/schemas/{{schemaId}}/collections?workspace=c90a3482-f80d-43d6-9dfb-0830f5e43605",
      "protocol": "https",
      "host": [
        "api",
        "getpostman",
        "com"
      ],
      "path": [
        "apis",
        "{{apiId}}",
        "versions",
        "{{apiVersionId}}",
        "schemas",
        "{{schemaId}}",
        "collections"
      ],
      "query": [
        {
          "key": "workspace",
          "value": "c90a3482-f80d-43d6-9dfb-0830f5e43605"
        }
      ]
    },
    "description": "This call creates a collection and links it to an API as one or multiple relations. \n\nRequest body requires two keys:\n- `name` - Name of the collection to be created.\n- `relations` - A list of relation(s) to be created.\n\nThe allowed relation types are `contracttest`, `integrationtest`, `testsuite`, and `documentation`.\n\n> Requires <a href=\"#authentication\">API Key</a> as `X-Api-Key` request header or `apikey` URL query parameter."
  },
  "response": [
    {
      "name": "Create collection from schema",
      "originalRequest": {
        "method": "POST",
        "header": [
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n\t\"name\": \"My generated collection\",\n\t\"relations\": [\n\t\t{\n\t\t\t\"type\": \"contracttest\"\n\t\t}\t\n\t]\n}",
          "options": {
            "raw": {
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "https://api.getpostman.com/apis/{{apiId}}/versions/{{apiVersionId}}/schemas/{{schemaId}}/collections?workspace=c90a3482-f80d-43d6-9dfb-0830f5e43605",
          "protocol": "https",
          "host": [
            "api",
            "getpostman",
            "com"
          ],
          "path": [
            "apis",
            "{{apiId}}",
            "versions",
            "{{apiVersionId}}",
            "schemas",
            "{{schemaId}}",
            "collections"
          ],
          "query": [
            {
              "key": "workspace",
              "value": "c90a3482-f80d-43d6-9dfb-0830f5e43605"
            }
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"collection\": {\n        \"id\": \"e6b0d46a-8722-4f42-ab86-f5f473187ddf\",\n        \"uid\": \"112098-e6b0d46a-8722-4f42-ab86-f5f473187ddf\"\n    },\n    \"relations\": [\n        {\n            \"type\": \"contracttest\",\n            \"id\": \"4b40f06a-5a6a-448f-bfcd-a6dbcb68da22\"\n        }\n    ]\n}"
    }
  ]
}