Chilkat Online Tools

Swift / Postman API / Create collection from schema

Back to Collection Items

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

    let http = CkoHttp()
    var success: Bool

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

    let json = CkoJsonObject()
    json.UpdateString("name", value: "My generated collection")
    json.UpdateString("relations[0].type", value: "contracttest")

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

    var resp: CkoHttpResponse? = http.PostJson3("https://api.getpostman.com/apis/{{apiId}}/versions/{{apiVersionId}}/schemas/{{schemaId}}/collections?workspace=c90a3482-f80d-43d6-9dfb-0830f5e43605", contentType: "application/json", json: json)
    if http.LastMethodSuccess == false {
        print("\(http.LastErrorText)")
        return
    }

    let sbResponseBody = CkoStringBuilder()
    resp!.GetBodySb(sbResponseBody)

    let jResp = CkoJsonObject()
    jResp.LoadSb(sbResponseBody)
    jResp.EmitCompact = false

    print("Response Body:")
    print("\(jResp.Emit())")

    var respStatusCode: Int = resp!.StatusCode.intValue
    print("Response Status Code = \(respStatusCode)")
    if respStatusCode >= 400 {
        print("Response Header:")
        print("\(resp!.Header)")
        print("Failed.")
        resp = nil
        return
    }

    resp = nil

    // 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

    var v_type: String?
    var id: String?

    var Id: String? = jResp.StringOf("collection.id")
    var Uid: String? = jResp.StringOf("collection.uid")
    var i: Int = 0
    var count_i: Int = jResp.SizeOfArray("relations").intValue
    while i < count_i {
        jResp.I = i
        v_type = jResp.StringOf("relations[i].type")
        id = jResp.StringOf("relations[i].id")
        i = i + 1
    }


}

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