VBScript / Postman API / Create collection from schema
Back to Collection Items
Dim fso, outFile
Set fso = CreateObject("Scripting.FileSystemObject")
'Create a Unicode (utf-16) output text file.
Set outFile = fso.CreateTextFile("output.txt", True, True)
' This example assumes the Chilkat API to have been previously unlocked.
' See Global Unlock Sample for sample code.
' For versions of Chilkat < 10.0.0, use CreateObject("Chilkat_9_5_0.Http")
set http = CreateObject("Chilkat.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"
' }
' ]
' }
' For versions of Chilkat < 10.0.0, use CreateObject("Chilkat_9_5_0.JsonObject")
set json = CreateObject("Chilkat.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.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
outFile.WriteLine(http.LastErrorText)
WScript.Quit
End If
' For versions of Chilkat < 10.0.0, use CreateObject("Chilkat_9_5_0.StringBuilder")
set sbResponseBody = CreateObject("Chilkat.StringBuilder")
success = resp.GetBodySb(sbResponseBody)
' For versions of Chilkat < 10.0.0, use CreateObject("Chilkat_9_5_0.JsonObject")
set jResp = CreateObject("Chilkat.JsonObject")
success = jResp.LoadSb(sbResponseBody)
jResp.EmitCompact = 0
outFile.WriteLine("Response Body:")
outFile.WriteLine(jResp.Emit())
respStatusCode = resp.StatusCode
outFile.WriteLine("Response Status Code = " & respStatusCode)
If (respStatusCode >= 400) Then
outFile.WriteLine("Response Header:")
outFile.WriteLine(resp.Header)
outFile.WriteLine("Failed.")
WScript.Quit
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
outFile.Close
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}"
}
]
}