Chilkat Online Tools

SQL Server / Postman API / Single 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
    -- 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

    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'X-API-Key', '{{postman_api_key}}'

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

    EXEC sp_OAMethod @http, 'QuickGetSb', @success OUT, 'https://api.getpostman.com/collections/{{collection_uid}}', @sbResponseBody
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @sbResponseBody
        RETURN
      END

    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 @http, 'LastStatus', @respStatusCode OUT

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

        PRINT 'Response Header:'
        EXEC sp_OAGetProperty @http, 'LastHeader', @sTmp0 OUT
        PRINT @sTmp0

        PRINT 'Failed.'
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @sbResponseBody
        EXEC @hr = sp_OADestroy @jResp
        RETURN
      END

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

    -- {
    --   "collection": {
    --     "variables": [
    --     ],
    --     "info": {
    --       "name": "Sample Collection",
    --       "_postman_id": "f2e66c2e-5297-e4a5-739e-20cbb90900e3",
    --       "description": "This is a sample collection that makes a tiny request to Postman Echo service to get the list of request headers sent by a HTTP client.",
    --       "schema": "https://schema.getpostman.com/json/collection/v2.0.0/collection.json"
    --     },
    --     "item": [
    --       {
    --         "id": "82ee981b-e19f-962a-401e-ea34ebfb4848",
    --         "name": "Request Headers",
    --         "event": [
    --           {
    --             "listen": "test",
    --             "script": {
    --               "type": "text/javascript",
    --               "exec": "var responseJSON;\ntry {\n    tests[\"Body contains headers\"] = responseBody.has(\"headers\");\n    responseJSON = JSON.parse(responseBody);\n    tests[\"Header contains host\"] = \"host\" in responseJSON.headers;\n    tests[\"Header contains test parameter sent as part of request header\"] = \"my-sample-header\" in responseJSON.headers;\n}\ncatch (e) { }\n\n\n\n"
    --             }
    --           }
    --         ],
    --         "request": {
    --           "url": "https://echo.getpostman.com/headers",
    --           "method": "GET",
    --           "header": [
    --             {
    --               "key": "my-sample-header",
    --               "value": "Lorem ipsum dolor sit amet",
    --               "description": ""
    --             }
    --           ],
    --           "body": {
    --             "mode": "formdata",
    --             "formdata": [
    --             ]
    --           },
    --           "description": ""
    --         },
    --         "response": [
    --         ]
    --       }
    --     ]
    --   }
    -- }

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

    DECLARE @name nvarchar(4000)

    DECLARE @v_Url nvarchar(4000)

    DECLARE @Method nvarchar(4000)

    DECLARE @Mode nvarchar(4000)

    DECLARE @requestDescription nvarchar(4000)

    DECLARE @j int

    DECLARE @count_j int

    DECLARE @listen nvarchar(4000)

    DECLARE @v_Type nvarchar(4000)

    DECLARE @Exec nvarchar(4000)

    DECLARE @key nvarchar(4000)

    DECLARE @value nvarchar(4000)

    DECLARE @description nvarchar(4000)

    DECLARE @Name nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Name OUT, 'collection.info.name'
    DECLARE @v_postman_id nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @v_postman_id OUT, 'collection.info._postman_id'
    DECLARE @Description nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Description OUT, 'collection.info.description'
    DECLARE @Schema nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Schema OUT, 'collection.info.schema'
    DECLARE @i int
    SELECT @i = 0
    DECLARE @count_i int
    EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_i OUT, 'collection.variables'
    WHILE @i < @count_i
      BEGIN
        EXEC sp_OASetProperty @jResp, 'I', @i
        SELECT @i = @i + 1
      END
    SELECT @i = 0
    EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_i OUT, 'collection.item'
    WHILE @i < @count_i
      BEGIN
        EXEC sp_OASetProperty @jResp, 'I', @i
        EXEC sp_OAMethod @jResp, 'StringOf', @id OUT, 'collection.item[i].id'
        EXEC sp_OAMethod @jResp, 'StringOf', @name OUT, 'collection.item[i].name'
        EXEC sp_OAMethod @jResp, 'StringOf', @v_Url OUT, 'collection.item[i].request.url'
        EXEC sp_OAMethod @jResp, 'StringOf', @Method OUT, 'collection.item[i].request.method'
        EXEC sp_OAMethod @jResp, 'StringOf', @Mode OUT, 'collection.item[i].request.body.mode'
        EXEC sp_OAMethod @jResp, 'StringOf', @requestDescription OUT, 'collection.item[i].request.description'
        SELECT @j = 0
        EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_j OUT, 'collection.item[i].event'
        WHILE @j < @count_j
          BEGIN
            EXEC sp_OASetProperty @jResp, 'J', @j
            EXEC sp_OAMethod @jResp, 'StringOf', @listen OUT, 'collection.item[i].event[j].listen'
            EXEC sp_OAMethod @jResp, 'StringOf', @v_Type OUT, 'collection.item[i].event[j].script.type'
            EXEC sp_OAMethod @jResp, 'StringOf', @Exec OUT, 'collection.item[i].event[j].script.exec'
            SELECT @j = @j + 1
          END
        SELECT @j = 0
        EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_j OUT, 'collection.item[i].request.header'
        WHILE @j < @count_j
          BEGIN
            EXEC sp_OASetProperty @jResp, 'J', @j
            EXEC sp_OAMethod @jResp, 'StringOf', @key OUT, 'collection.item[i].request.header[j].key'
            EXEC sp_OAMethod @jResp, 'StringOf', @value OUT, 'collection.item[i].request.header[j].value'
            EXEC sp_OAMethod @jResp, 'StringOf', @description OUT, 'collection.item[i].request.header[j].description'
            SELECT @j = @j + 1
          END
        SELECT @j = 0
        EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_j OUT, 'collection.item[i].request.body.formdata'
        WHILE @j < @count_j
          BEGIN
            EXEC sp_OASetProperty @jResp, 'J', @j
            SELECT @j = @j + 1
          END
        SELECT @j = 0
        EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_j OUT, 'collection.item[i].response'
        WHILE @j < @count_j
          BEGIN
            EXEC sp_OASetProperty @jResp, 'J', @j
            SELECT @j = @j + 1
          END
        SELECT @i = @i + 1
      END

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


END
GO

Curl Command

curl -X GET
	-H "X-API-Key: {{postman_api_key}}"
https://api.getpostman.com/collections/{{collection_uid}}

Postman Collection Item JSON

{
  "name": "Single 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');",
          "});",
          "",
          "// 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');",
          "});",
          "",
          "// you can unskip this test in case you want to have a stricter check on the selected",
          "// environment.",
          "pm.test.skip('\"collection_uid\" variable should be present', function () {",
          "    pm.expect(pm.variables.get('collection_uid')).to.be.a('string');",
          "});"
        ]
      }
    }
  ],
  "request": {
    "method": "GET",
    "header": [
    ],
    "url": {
      "raw": "https://api.getpostman.com/collections/{{collection_uid}}",
      "protocol": "https",
      "host": [
        "api",
        "getpostman",
        "com"
      ],
      "path": [
        "collections",
        "{{collection_uid}}"
      ]
    },
    "description": "Access the contents of a collection that is accessible to you using its unique id (`uid`).\n\n> Requires <a href=\"#authentication\">API Key</a> as `X-Api-Key` request header or `apikey` URL query parameter."
  },
  "response": [
    {
      "name": "Valid Response",
      "originalRequest": {
        "method": "GET",
        "header": [
        ],
        "url": {
          "raw": "https://api.getpostman.com/collections/{{collection_uid}}",
          "protocol": "https",
          "host": [
            "api",
            "getpostman",
            "com"
          ],
          "path": [
            "collections",
            "{{collection_uid}}"
          ]
        }
      },
      "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        \"variables\": [],\n        \"info\": {\n            \"name\": \"Sample Collection\",\n            \"_postman_id\": \"f2e66c2e-5297-e4a5-739e-20cbb90900e3\",\n            \"description\": \"This is a sample collection that makes a tiny request to Postman Echo service to get the list of request headers sent by a HTTP client.\",\n            \"schema\": \"https://schema.getpostman.com/json/collection/v2.0.0/collection.json\"\n        },\n        \"item\": [\n            {\n                \"id\": \"82ee981b-e19f-962a-401e-ea34ebfb4848\",\n                \"name\": \"Request Headers\",\n                \"event\": [\n                    {\n                        \"listen\": \"test\",\n                        \"script\": {\n                            \"type\": \"text/javascript\",\n                            \"exec\": \"var responseJSON;\\ntry {\\n    tests[\\\"Body contains headers\\\"] = responseBody.has(\\\"headers\\\");\\n    responseJSON = JSON.parse(responseBody);\\n    tests[\\\"Header contains host\\\"] = \\\"host\\\" in responseJSON.headers;\\n    tests[\\\"Header contains test parameter sent as part of request header\\\"] = \\\"my-sample-header\\\" in responseJSON.headers;\\n}\\ncatch (e) { }\\n\\n\\n\\n\"\n                        }\n                    }\n                ],\n                \"request\": {\n                    \"url\": \"https://echo.getpostman.com/headers\",\n                    \"method\": \"GET\",\n                    \"header\": [\n                        {\n                            \"key\": \"my-sample-header\",\n                            \"value\": \"Lorem ipsum dolor sit amet\",\n                            \"description\": \"\"\n                        }\n                    ],\n                    \"body\": {\n                        \"mode\": \"formdata\",\n                        \"formdata\": []\n                    },\n                    \"description\": \"\"\n                },\n                \"response\": []\n            }\n        ]\n    }\n}"
    }
  ]
}