Chilkat Online Tools

SQL Server / Postman API / Create Collection

Back to Collection Items

-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    -- This example assumes the Chilkat API to have been previously unlocked.
    -- See Global Unlock Sample for sample code.

    DECLARE @http int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.Http', @http OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    DECLARE @success int

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

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

    -- {
    --   "collection": {
    --     "info": {
    --       "name": "Sample Collection 909",
    --       "description": "This is just a sample collection.",
    --       "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
    --     },
    --     "item": [
    --       {
    --         "name": "This is a folder",
    --         "item": [
    --           {
    --             "name": "Sample POST Request",
    --             "request": {
    --               "url": "https://postman-echo.com/post",
    --               "method": "POST",
    --               "header": [
    --                 {
    --                   "key": "Content-Type",
    --                   "value": "application/json"
    --                 }
    --               ],
    --               "body": {
    --                 "mode": "raw",
    --                 "raw": "{\"data\": \"123\"}"
    --               },
    --               "description": "This is a sample POST Request"
    --             }
    --           }
    --         ]
    --       },
    --       {
    --         "name": "Sample GET Request",
    --         "request": {
    --           "url": "https://postman-echo/get",
    --           "method": "GET",
    --           "description": "This is a sample GET Request"
    --         }
    --       }
    --     ]
    --   }
    -- }

    DECLARE @json int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.JsonObject', @json OUT

    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'collection.info.name', 'Sample Collection 909'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'collection.info.description', 'This is just a sample collection.'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'collection.info.schema', 'https://schema.getpostman.com/json/collection/v2.1.0/collection.json'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'collection.item[0].name', 'This is a folder'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'collection.item[0].item[0].name', 'Sample POST Request'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'collection.item[0].item[0].request.url', 'https://postman-echo.com/post'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'collection.item[0].item[0].request.method', 'POST'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'collection.item[0].item[0].request.header[0].key', 'Content-Type'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'collection.item[0].item[0].request.header[0].value', 'application/json'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'collection.item[0].item[0].request.body.mode', 'raw'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'collection.item[0].item[0].request.body.raw', '{"data": "123"}'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'collection.item[0].item[0].request.description', 'This is a sample POST Request'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'collection.item[1].name', 'Sample GET Request'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'collection.item[1].request.url', 'https://postman-echo/get'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'collection.item[1].request.method', 'GET'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'collection.item[1].request.description', 'This is a sample GET Request'

    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'Content-Type', 'application/json'
    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'X-API-Key', '{{postman_api_key}}'

    DECLARE @resp int
    EXEC sp_OAMethod @http, 'PostJson3', @resp OUT, 'https://api.getpostman.com/collections', 'application/json', @json
    EXEC sp_OAGetProperty @http, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 = 0
      BEGIN
        EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @json
        RETURN
      END

    DECLARE @sbResponseBody int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.StringBuilder', @sbResponseBody OUT

    EXEC sp_OAMethod @resp, 'GetBodySb', @success OUT, @sbResponseBody

    DECLARE @jResp int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.JsonObject', @jResp OUT

    EXEC sp_OAMethod @jResp, 'LoadSb', @success OUT, @sbResponseBody
    EXEC sp_OASetProperty @jResp, 'EmitCompact', 0


    PRINT 'Response Body:'
    EXEC sp_OAMethod @jResp, 'Emit', @sTmp0 OUT
    PRINT @sTmp0

    DECLARE @respStatusCode int
    EXEC sp_OAGetProperty @resp, 'StatusCode', @respStatusCode OUT

    PRINT 'Response Status Code = ' + @respStatusCode
    IF @respStatusCode >= 400
      BEGIN

        PRINT 'Response Header:'
        EXEC sp_OAGetProperty @resp, 'Header', @sTmp0 OUT
        PRINT @sTmp0

        PRINT 'Failed.'
        EXEC @hr = sp_OADestroy @resp

        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @json
        EXEC @hr = sp_OADestroy @sbResponseBody
        EXEC @hr = sp_OADestroy @jResp
        RETURN
      END
    EXEC @hr = sp_OADestroy @resp

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

    -- {
    --   "collection": {
    --     "id": "2412a72c-1d8e-491b-aced-93809c0e94e9",
    --     "name": "Sample Collection",
    --     "uid": "5852-2412a72c-1d8e-491b-aced-93809c0e94e9"
    --   }
    -- }

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

    DECLARE @Id nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Id OUT, 'collection.id'
    DECLARE @Name nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Name OUT, 'collection.name'
    DECLARE @Uid nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Uid OUT, 'collection.uid'

    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @json
    EXEC @hr = sp_OADestroy @sbResponseBody
    EXEC @hr = sp_OADestroy @jResp


END
GO

Curl Command

curl -X POST
	-H "X-API-Key: {{postman_api_key}}"
	-H "Content-Type: application/json"
	-d '{
    "collection": {
        "info": {
            "name": "Sample Collection 909",
            "description": "This is just a sample collection.",
            "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
        },
        "item": [
            {
                "name": "This is a folder",
                "item": [
                    {
                        "name": "Sample POST Request",
                        "request": {
                            "url": "https://postman-echo.com/post",
                            "method": "POST",
                            "header": [
                                {
                                    "key": "Content-Type",
                                    "value": "application/json"
                                }
                            ],
                            "body": {
                                "mode": "raw",
                                "raw": "{\"data\": \"123\"}"
                            },
                            "description": "This is a sample POST Request"
                        }
                    }
                ]
            },
            {
                "name": "Sample GET Request",
                "request": {
                    "url": "https://postman-echo/get",
                    "method": "GET",
                    "description": "This is a sample GET Request"
                }
            }
        ]
    }
}'
https://api.getpostman.com/collections

Postman Collection Item JSON

{
  "name": "Create Collection",
  "event": [
    {
      "listen": "test",
      "script": {
        "type": "text/javascript",
        "exec": [
          "pm.test(\"response is ok\", function () {",
          "    pm.response.to.have.status(200);",
          "});",
          "",
          "pm.test(\"response json data should not have errors\", function () {",
          "    pm.response.to.have.jsonBody();",
          "    pm.response.to.not.have.jsonBody('error');",
          "});",
          "",
          "pm.test(\"response json should contain one collection\", function () {",
          "    pm.expect(pm.response.json()).to.have.property('collection').and.be.an('object');",
          "});",
          "",
          "pm.test(\"response json should return collection details\", function () {",
          "    pm.expect(pm.response.json()).to.have.property('collection')",
          "        .and.to.include.keys(['id', 'name', 'uid']);",
          "});",
          "",
          "// additional tests to ensure that the relevant environment variables are present",
          "pm.test('\"postman_api_key\" variable should be present', function () {",
          "    pm.expect(pm.variables.get('postman_api_key')).to.be.a('string');",
          "});"
        ]
      }
    }
  ],
  "request": {
    "method": "POST",
    "header": [
      {
        "key": "Content-Type",
        "value": "application/json"
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n    \"collection\": {\n        \"info\": {\n            \"name\": \"Sample Collection 909\",\n            \"description\": \"This is just a sample collection.\",\n            \"schema\": \"https://schema.getpostman.com/json/collection/v2.1.0/collection.json\"\n        },\n        \"item\": [\n            {\n                \"name\": \"This is a folder\",\n                \"item\": [\n                    {\n                        \"name\": \"Sample POST Request\",\n                        \"request\": {\n                            \"url\": \"https://postman-echo.com/post\",\n                            \"method\": \"POST\",\n                            \"header\": [\n                                {\n                                    \"key\": \"Content-Type\",\n                                    \"value\": \"application/json\"\n                                }\n                            ],\n                            \"body\": {\n                                \"mode\": \"raw\",\n                                \"raw\": \"{\\\"data\\\": \\\"123\\\"}\"\n                            },\n                            \"description\": \"This is a sample POST Request\"\n                        }\n                    }\n                ]\n            },\n            {\n                \"name\": \"Sample GET Request\",\n                \"request\": {\n                    \"url\": \"https://postman-echo/get\",\n                    \"method\": \"GET\",\n                    \"description\": \"This is a sample GET Request\"\n                }\n            }\n        ]\n    }\n}"
    },
    "url": {
      "raw": "https://api.getpostman.com/collections",
      "protocol": "https",
      "host": [
        "api",
        "getpostman",
        "com"
      ],
      "path": [
        "collections"
      ]
    },
    "description": "This endpoint allows you to create collections using the Postman Collection v2 format. For more information about the v2 schema, check the format [here](https://schema.getpostman.com/json/collection/v2.0.0/docs/index.html).\n\nOn successful creation of the collection, the response returns the collection `name`, `id` and the `uid`.\n\nYou can also specify the context of a workspace to create a collection 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": "Collection Already Exists",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "name": "Content-Type",
            "key": "Content-Type",
            "value": "application/json"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n    \"collection\": {\n        \"variables\": [],\n        \"info\": {\n            \"_postman_id\": \"ac03df1d-90f0-401d-aa57-39c395253c80\",\n            \"name\": \"Sample Collection\",\n            \"description\": \"This is just a sample collection.\",\n            \"schema\": \"https://schema.getpostman.com/json/collection/v2.0.0/collection.json\"\n        },\n        \"item\": [\n            {\n                \"name\": \"This is a folder\",\n                \"description\": \"\",\n                \"item\": [\n                    {\n                        \"name\": \"Sample POST Request\",\n                        \"request\": {\n                            \"url\": \"echo.getpostman.com/post\",\n                            \"method\": \"POST\",\n                            \"header\": [\n                                {\n                                    \"key\": \"Content-Type\",\n                                    \"value\": \"application/json\",\n                                    \"description\": \"\"\n                                }\n                            ],\n                            \"body\": {\n                                \"mode\": \"raw\",\n                                \"raw\": \"{\\n\\t\\\"data\\\": \\\"123\\\"\\n}\"\n                            },\n                            \"description\": \"This is a sample POST Request\"\n                        },\n                        \"response\": []\n                    }\n                ]\n            },\n            {\n                \"name\": \"Sample GET Request\",\n                \"request\": {\n                    \"url\": \"echo.getpostman.com/get\",\n                    \"method\": \"GET\",\n                    \"header\": [],\n                    \"body\": {\n                        \"mode\": \"formdata\",\n                        \"formdata\": []\n                    },\n                    \"description\": \"This is a sample GET Request\"\n                },\n                \"response\": []\n            }\n        ]\n    }\n}",
          "options": {
            "raw": {
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "https://api.getpostman.com/collections",
          "protocol": "https",
          "host": [
            "api",
            "getpostman",
            "com"
          ],
          "path": [
            "collections"
          ]
        }
      },
      "status": "Bad Request",
      "code": 400,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json",
          "name": "Content-Type",
          "description": {
            "content": "",
            "type": "text/plain"
          }
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"error\": {\n        \"name\": \"instanceFoundError\",\n        \"message\": \"The specified item already exists.\",\n        \"details\": {\n            \"item\": \"collection\",\n            \"id\": \"ac03df1d-90f0-401d-aa57-39c395253c80\"\n        }\n    }\n}"
    },
    {
      "name": "Collection Created",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "name": "Content-Type",
            "key": "Content-Type",
            "value": "application/json"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n    \"collection\": {\n        \"variables\": [],\n        \"info\": {\n            \"name\": \"Sample Collection\",\n            \"description\": \"This is just a sample collection.\",\n            \"schema\": \"https://schema.getpostman.com/json/collection/v2.0.0/collection.json\"\n        },\n        \"item\": [\n            {\n                \"name\": \"This is a folder\",\n                \"description\": \"\",\n                \"item\": [\n                    {\n                        \"name\": \"Sample POST Request\",\n                        \"request\": {\n                            \"url\": \"echo.getpostman.com/post\",\n                            \"method\": \"POST\",\n                            \"header\": [\n                                {\n                                    \"key\": \"Content-Type\",\n                                    \"value\": \"application/json\",\n                                    \"description\": \"\"\n                                }\n                            ],\n                            \"body\": {\n                                \"mode\": \"raw\",\n                                \"raw\": \"{\\n\\t\\\"data\\\": \\\"123\\\"\\n}\"\n                            },\n                            \"description\": \"This is a sample POST Request\"\n                        },\n                        \"response\": []\n                    }\n                ]\n            },\n            {\n                \"name\": \"Sample GET Request\",\n                \"request\": {\n                    \"url\": \"echo.getpostman.com/get\",\n                    \"method\": \"GET\",\n                    \"header\": [],\n                    \"body\": {\n                        \"mode\": \"formdata\",\n                        \"formdata\": []\n                    },\n                    \"description\": \"This is a sample GET Request\"\n                },\n                \"response\": []\n            }\n        ]\n    }\n}",
          "options": {
            "raw": {
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "https://api.getpostman.com/collections",
          "protocol": "https",
          "host": [
            "api",
            "getpostman",
            "com"
          ],
          "path": [
            "collections"
          ]
        }
      },
      "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  \"collection\": {\n    \"id\": \"2412a72c-1d8e-491b-aced-93809c0e94e9\",\n    \"name\": \"Sample Collection\",\n    \"uid\": \"5852-2412a72c-1d8e-491b-aced-93809c0e94e9\"\n  }\n}"
    },
    {
      "name": "Create Collection in a Workspace",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "name": "Content-Type",
            "key": "Content-Type",
            "value": "application/json"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n    \"collection\": {\n        \"info\": {\n            \"name\": \"Sample Collection 65\",\n            \"description\": \"This is just a sample collection.\",\n            \"schema\": \"https://schema.getpostman.com/json/collection/v2.1.0/collection.json\"\n        },\n        \"item\": [\n            {\n                \"name\": \"This is a folder\",\n                \"item\": [\n                    {\n                        \"name\": \"Sample POST Request\",\n                        \"request\": {\n                            \"url\": \"https://postman-echo.com/post\",\n                            \"method\": \"POST\",\n                            \"header\": [\n                                {\n                                    \"key\": \"Content-Type\",\n                                    \"value\": \"application/json\"\n                                }\n                            ],\n                            \"body\": {\n                                \"mode\": \"raw\",\n                                \"raw\": \"{\\\"data\\\": \\\"123\\\"}\"\n                            },\n                            \"description\": \"This is a sample POST Request\"\n                        }\n                    }\n                ]\n            },\n            {\n                \"name\": \"Sample GET Request\",\n                \"request\": {\n                    \"url\": \"https://postman-echo/get\",\n                    \"method\": \"GET\",\n                    \"description\": \"This is a sample GET Request\"\n                }\n            }\n        ]\n    }\n}",
          "options": {
            "raw": {
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "https://api.getpostman.com/collections?workspace={{workspace_id}}",
          "protocol": "https",
          "host": [
            "api",
            "getpostman",
            "com"
          ],
          "path": [
            "collections"
          ],
          "query": [
            {
              "key": "workspace",
              "value": "{{workspace_id}}",
              "description": "The workspace ID in which the collection is to 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    \"collection\": {\n        \"id\": \"2412a72c-1d8e-491b-aced-93809c0e94e9\",\n        \"name\": \"Sample Collection\",\n        \"uid\": \"5852-2412a72c-1d8e-491b-aced-93809c0e94e9\"\n    }\n}"
    },
    {
      "name": "Malformed Request",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "name": "Content-Type",
            "value": "application/json",
            "type": "text"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n    \"collection\": {\n        \"item\": [\n            {\n                \"name\": \"This is a folder\",\n                \"item\": [\n                    {\n                        \"name\": \"Sample POST Request\",\n                        \"request\": {\n                            \"url\": \"https://postman-echo.com/post\",\n                            \"method\": \"POST\",\n                            \"header\": [\n                                {\n                                    \"key\": \"Content-Type\",\n                                    \"value\": \"application/json\"\n                                }\n                            ],\n                            \"body\": {\n                                \"mode\": \"raw\",\n                                \"raw\": \"{\\\"data\\\": \\\"123\\\"}\"\n                            },\n                            \"description\": \"This is a sample POST Request\"\n                        }\n                    }\n                ]\n            },\n            {\n                \"name\": \"Sample GET Request\",\n                \"request\": {\n                    \"url\": \"https://postman-echo/get\",\n                    \"method\": \"GET\",\n                    \"header\": {},\n                    \"description\": \"This is a sample GET Request\"\n                }\n            }\n        ]\n    }\n}",
          "options": {
            "raw": {
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "https://api.getpostman.com/collections",
          "protocol": "https",
          "host": [
            "api",
            "getpostman",
            "com"
          ],
          "path": [
            "collections"
          ]
        }
      },
      "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 8 errors with the supplied collection.\",\n        \"details\": [\n            \": must have required property 'info'\",\n            \"item/1/request/header: must be array\",\n            \"item/1/request/header: must be string\",\n            \"item/1/request/header: must match exactly one schema in oneOf\",\n            \"item/1/request: must be string\",\n            \"item/1/request: must match exactly one schema in oneOf\",\n            \"item/1: must have required property 'item'\",\n            \"item/1: must match exactly one schema in oneOf\"\n        ]\n    }\n}"
    }
  ]
}