Chilkat Online Tools

VB6 / Postman API / Create collection from schema

Back to Collection Items

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

Dim http As New ChilkatHttp
Dim success As Long

' 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"
'     }
'   ]
' }

Dim json As New ChilkatJsonObject
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}}"

Dim resp As ChilkatHttpResponse
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
    Debug.Print http.LastErrorText
    Exit Sub
End If

Dim sbResponseBody As New ChilkatStringBuilder
success = resp.GetBodySb(sbResponseBody)

Dim jResp As New ChilkatJsonObject
success = jResp.LoadSb(sbResponseBody)
jResp.EmitCompact = 0

Debug.Print "Response Body:"
Debug.Print jResp.Emit()

Dim respStatusCode As Long
respStatusCode = resp.StatusCode
Debug.Print "Response Status Code = " & respStatusCode
If (respStatusCode >= 400) Then
    Debug.Print "Response Header:"
    Debug.Print resp.Header
    Debug.Print "Failed."

    Exit Sub
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

Dim v_type As String
Dim id As String

Dim Id As String
Id = jResp.StringOf("collection.id")
Dim Uid As String
Uid = jResp.StringOf("collection.uid")
Dim i As Long
i = 0
Dim count_i As Long
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

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}"
    }
  ]
}