Chilkat Online Tools

PureBasic / Postman API / All Mocks

Back to Collection Items

IncludeFile "CkJsonObject.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkHttp.pb"

Procedure ChilkatExample()

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

    http.i = CkHttp::ckCreate()
    If http.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success.i

    CkHttp::ckSetRequestHeader(http,"X-API-Key","{{postman_api_key}}")

    sbResponseBody.i = CkStringBuilder::ckCreate()
    If sbResponseBody.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkHttp::ckQuickGetSb(http,"https://api.getpostman.com/mocks",sbResponseBody)
    If success = 0
        Debug CkHttp::ckLastErrorText(http)
        CkHttp::ckDispose(http)
        CkStringBuilder::ckDispose(sbResponseBody)
        ProcedureReturn
    EndIf

    jResp.i = CkJsonObject::ckCreate()
    If jResp.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckLoadSb(jResp,sbResponseBody)
    CkJsonObject::setCkEmitCompact(jResp, 0)

    Debug "Response Body:"
    Debug CkJsonObject::ckEmit(jResp)

    respStatusCode.i = CkHttp::ckLastStatus(http)
    Debug "Response Status Code = " + Str(respStatusCode)
    If respStatusCode >= 400
        Debug "Response Header:"
        Debug CkHttp::ckLastHeader(http)
        Debug "Failed."
        CkHttp::ckDispose(http)
        CkStringBuilder::ckDispose(sbResponseBody)
        CkJsonObject::ckDispose(jResp)
        ProcedureReturn
    EndIf

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

    ; {
    ;   "mocks": [
    ;     {
    ;       "id": "e782b64e-406b-4a6c-8fe9-9ebe84aeb706",
    ;       "owner": "1686168",
    ;       "uid": "1686168-e782b64e-406b-4a6c-8fe9-9ebe84aeb706",
    ;       "collection": "1686168-39fee52f-b806-3ffa-1173-00a6f5b183dc",
    ;       "environment": "1686168-2b90cdd8-e9ac-4086-91c2-bc4cad659511",
    ;       "mockUrl": "https://e782b64e-406b-4a6c-8fe9-9ebe84aeb706.mock.pstmn.io",
    ;       "name": "mock1",
    ;       "config": {
    ;         "headers": [
    ;         ],
    ;         "matchBody": false,
    ;         "matchQueryParams": true,
    ;         "matchWildcards": true,
    ;         "delay": null
    ;       },
    ;       "createdAt": "2020-12-15T06:44:37.000Z",
    ;       "updatedAt": "2020-12-15T06:44:37.000Z",
    ;       "isPublic": false
    ;     },
    ;     {
    ;       "id": "8f9d4b95-780d-455d-af38-772277bc9393",
    ;       "owner": "1686168",
    ;       "uid": "1686168-8f9d4b95-780d-455d-af38-772277bc9393",
    ;       "collection": "1686168-37eeb72f-5d3e-48b4-8022-6b9cca6d2664",
    ;       "mockUrl": "https://8f9d4b95-780d-455d-af38-772277bc93936.mock.pstmn.io",
    ;       "name": "mock2",
    ;       "config": {
    ;         "headers": [
    ;         ],
    ;         "matchBody": false,
    ;         "matchQueryParams": true,
    ;         "matchWildcards": true,
    ;         "delay": null
    ;       },
    ;       "createdAt": "2020-11-15T06:44:37.000Z",
    ;       "updatedAt": "2020-12-15T06:48:37.000Z",
    ;       "isPublic": true
    ;     }
    ;   ]
    ; }

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

    id.s
    owner.s
    uid.s
    collection.s
    environment.s
    mockUrl.s
    name.s
    MatchBody.i
    MatchQueryParams.i
    MatchWildcards.i
    Delay.s
    createdAt.s
    updatedAt.s
    isPublic.i
    j.i
    count_j.i

    i.i = 0
    count_i.i = CkJsonObject::ckSizeOfArray(jResp,"mocks")
    While i < count_i
        CkJsonObject::setCkI(jResp, i)
        id = CkJsonObject::ckStringOf(jResp,"mocks[i].id")
        owner = CkJsonObject::ckStringOf(jResp,"mocks[i].owner")
        uid = CkJsonObject::ckStringOf(jResp,"mocks[i].uid")
        collection = CkJsonObject::ckStringOf(jResp,"mocks[i].collection")
        environment = CkJsonObject::ckStringOf(jResp,"mocks[i].environment")
        mockUrl = CkJsonObject::ckStringOf(jResp,"mocks[i].mockUrl")
        name = CkJsonObject::ckStringOf(jResp,"mocks[i].name")
        MatchBody = CkJsonObject::ckBoolOf(jResp,"mocks[i].config.matchBody")
        MatchQueryParams = CkJsonObject::ckBoolOf(jResp,"mocks[i].config.matchQueryParams")
        MatchWildcards = CkJsonObject::ckBoolOf(jResp,"mocks[i].config.matchWildcards")
        Delay = CkJsonObject::ckStringOf(jResp,"mocks[i].config.delay")
        createdAt = CkJsonObject::ckStringOf(jResp,"mocks[i].createdAt")
        updatedAt = CkJsonObject::ckStringOf(jResp,"mocks[i].updatedAt")
        isPublic = CkJsonObject::ckBoolOf(jResp,"mocks[i].isPublic")
        j = 0
        count_j = CkJsonObject::ckSizeOfArray(jResp,"mocks[i].config.headers")
        While j < count_j
            CkJsonObject::setCkJ(jResp, j)
            j = j + 1
        Wend
        i = i + 1
    Wend


    CkHttp::ckDispose(http)
    CkStringBuilder::ckDispose(sbResponseBody)
    CkJsonObject::ckDispose(jResp)


    ProcedureReturn
EndProcedure

Curl Command

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

Postman Collection Item JSON

{
  "name": "All Mocks",
  "request": {
    "method": "GET",
    "header": [
    ],
    "url": {
      "raw": "https://api.getpostman.com/mocks",
      "protocol": "https",
      "host": [
        "api",
        "getpostman",
        "com"
      ],
      "path": [
        "mocks"
      ]
    },
    "description": "This endpoint fetches all the mocks that you have created.\n\n> Requires <a href=\"#authentication\">API Key</a> as `X-Api-Key` request header or `apikey` URL query parameter."
  },
  "response": [
    {
      "name": "Successful Response",
      "originalRequest": {
        "method": "GET",
        "header": [
        ],
        "url": {
          "raw": "https://api.getpostman.com/mocks",
          "protocol": "https",
          "host": [
            "api",
            "getpostman",
            "com"
          ],
          "path": [
            "mocks"
          ]
        }
      },
      "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    \"mocks\": [\n        {\n            \"id\": \"e782b64e-406b-4a6c-8fe9-9ebe84aeb706\",\n            \"owner\": \"1686168\",\n            \"uid\": \"1686168-e782b64e-406b-4a6c-8fe9-9ebe84aeb706\",\n            \"collection\": \"1686168-39fee52f-b806-3ffa-1173-00a6f5b183dc\",\n            \"environment\": \"1686168-2b90cdd8-e9ac-4086-91c2-bc4cad659511\",\n            \"mockUrl\": \"https://e782b64e-406b-4a6c-8fe9-9ebe84aeb706.mock.pstmn.io\",\n            \"name\": \"mock1\",\n            \"config\": {\n                \"headers\": [],\n                \"matchBody\": false,\n                \"matchQueryParams\": true,\n                \"matchWildcards\": true,\n                \"delay\": null\n            },\n            \"createdAt\": \"2020-12-15T06:44:37.000Z\",\n            \"updatedAt\": \"2020-12-15T06:44:37.000Z\",\n            \"isPublic\": false\n        },\n        {\n            \"id\": \"8f9d4b95-780d-455d-af38-772277bc9393\",\n            \"owner\": \"1686168\",\n            \"uid\": \"1686168-8f9d4b95-780d-455d-af38-772277bc9393\",\n            \"collection\": \"1686168-37eeb72f-5d3e-48b4-8022-6b9cca6d2664\",\n            \"mockUrl\": \"https://8f9d4b95-780d-455d-af38-772277bc93936.mock.pstmn.io\",\n            \"name\": \"mock2\",\n            \"config\": {\n                \"headers\": [],\n                \"matchBody\": false,\n                \"matchQueryParams\": true,\n                \"matchWildcards\": true,\n                \"delay\": null\n            },\n            \"createdAt\": \"2020-11-15T06:44:37.000Z\",\n            \"updatedAt\": \"2020-12-15T06:48:37.000Z\",\n            \"isPublic\": true\n        }\n    ]\n}"
    }
  ]
}