Chilkat Online Tools

unicodeC / Postman API / Update Mock

Back to Collection Items

#include <C_CkHttpW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkStringBuilderW.h>
#include <C_CkHttpResponseW.h>

void ChilkatSample(void)
    {
    HCkHttpW http;
    BOOL success;
    HCkJsonObjectW json;
    HCkStringBuilderW sbRequestBody;
    HCkHttpResponseW resp;
    HCkStringBuilderW sbResponseBody;
    HCkJsonObjectW jResp;
    int respStatusCode;
    const wchar_t *Id;
    const wchar_t *Owner;
    const wchar_t *Uid;
    const wchar_t *Collection;
    const wchar_t *MockUrl;
    const wchar_t *Name;
    BOOL MatchBody;
    BOOL MatchQueryParams;
    BOOL MatchWildcards;
    const wchar_t *Environment;
    int i;
    int count_i;

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

    http = CkHttpW_Create();

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

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

    // {
    //   "mock": {
    //     "name": "My Mock Server",
    //     "environment": "11582779-ac1b6608-deb7-4c05-9d48-ee775aabfc19",
    //     "description": "example describing update mock description",
    //     "private": false,
    //     "versionTag": "abf07d3d-f8ec-47d4-8015-9fe83078b4ec"
    //   }
    // }

    json = CkJsonObjectW_Create();
    CkJsonObjectW_UpdateString(json,L"mock.name",L"My Mock Server");
    CkJsonObjectW_UpdateString(json,L"mock.environment",L"11582779-ac1b6608-deb7-4c05-9d48-ee775aabfc19");
    CkJsonObjectW_UpdateString(json,L"mock.description",L"example describing update mock description");
    CkJsonObjectW_UpdateBool(json,L"mock.private",FALSE);
    CkJsonObjectW_UpdateString(json,L"mock.versionTag",L"abf07d3d-f8ec-47d4-8015-9fe83078b4ec");

    CkHttpW_SetRequestHeader(http,L"Content-Type",L"application/json");
    CkHttpW_SetRequestHeader(http,L"X-API-Key",L"{{postman_api_key}}");

    sbRequestBody = CkStringBuilderW_Create();
    CkJsonObjectW_EmitSb(json,sbRequestBody);

    resp = CkHttpW_PTextSb(http,L"PUT",L"https://api.getpostman.com/mocks/{{mock_uid}}",sbRequestBody,L"utf-8",L"application/json",FALSE,FALSE);
    if (CkHttpW_getLastMethodSuccess(http) == FALSE) {
        wprintf(L"%s\n",CkHttpW_lastErrorText(http));
        CkHttpW_Dispose(http);
        CkJsonObjectW_Dispose(json);
        CkStringBuilderW_Dispose(sbRequestBody);
        return;
    }

    sbResponseBody = CkStringBuilderW_Create();
    CkHttpResponseW_GetBodySb(resp,sbResponseBody);

    jResp = CkJsonObjectW_Create();
    CkJsonObjectW_LoadSb(jResp,sbResponseBody);
    CkJsonObjectW_putEmitCompact(jResp,FALSE);

    wprintf(L"Response Body:\n");
    wprintf(L"%s\n",CkJsonObjectW_emit(jResp));

    respStatusCode = CkHttpResponseW_getStatusCode(resp);
    wprintf(L"Response Status Code = %d\n",respStatusCode);
    if (respStatusCode >= 400) {
        wprintf(L"Response Header:\n");
        wprintf(L"%s\n",CkHttpResponseW_header(resp));
        wprintf(L"Failed.\n");
        CkHttpResponseW_Dispose(resp);
        CkHttpW_Dispose(http);
        CkJsonObjectW_Dispose(json);
        CkStringBuilderW_Dispose(sbRequestBody);
        CkStringBuilderW_Dispose(sbResponseBody);
        CkJsonObjectW_Dispose(jResp);
        return;
    }

    CkHttpResponseW_Dispose(resp);

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

    // {
    //   "mock": {
    //     "id": "06040138-dd6b-4cce-9a02-7e1c1ab59723",
    //     "owner": "11582779",
    //     "uid": "11582779-06040138-dd6b-4cce-9a02-7e1c1ab59723",
    //     "collection": "11582779-fd6797e2-de6b-4699-975c-85290e4c2499",
    //     "mockUrl": "https://06040138-dd6b-4cce-9a02-7e1c1ab59723.mock.pstmn.io",
    //     "name": "My Mock Server",
    //     "config": {
    //       "headers": [
    //       ],
    //       "matchBody": false,
    //       "matchQueryParams": true,
    //       "matchWildcards": true
    //     },
    //     "environment": "11582779-ac1b6608-deb7-4c05-9d48-ee775aabfc19"
    //   }
    // }

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

    // Chilkat functions returning "const char *" return a pointer to temporary internal memory owned and managed by Chilkat.

    Id = CkJsonObjectW_stringOf(jResp,L"mock.id");
    Owner = CkJsonObjectW_stringOf(jResp,L"mock.owner");
    Uid = CkJsonObjectW_stringOf(jResp,L"mock.uid");
    Collection = CkJsonObjectW_stringOf(jResp,L"mock.collection");
    MockUrl = CkJsonObjectW_stringOf(jResp,L"mock.mockUrl");
    Name = CkJsonObjectW_stringOf(jResp,L"mock.name");
    MatchBody = CkJsonObjectW_BoolOf(jResp,L"mock.config.matchBody");
    MatchQueryParams = CkJsonObjectW_BoolOf(jResp,L"mock.config.matchQueryParams");
    MatchWildcards = CkJsonObjectW_BoolOf(jResp,L"mock.config.matchWildcards");
    Environment = CkJsonObjectW_stringOf(jResp,L"mock.environment");
    i = 0;
    count_i = CkJsonObjectW_SizeOfArray(jResp,L"mock.config.headers");
    while (i < count_i) {
        CkJsonObjectW_putI(jResp,i);
        i = i + 1;
    }



    CkHttpW_Dispose(http);
    CkJsonObjectW_Dispose(json);
    CkStringBuilderW_Dispose(sbRequestBody);
    CkStringBuilderW_Dispose(sbResponseBody);
    CkJsonObjectW_Dispose(jResp);

    }

Curl Command

curl -X PUT
	-H "X-API-Key: {{postman_api_key}}"
	-H "Content-Type: application/json"
	-d '{
    "mock": {
        "name": "My Mock Server",
        "environment": "11582779-ac1b6608-deb7-4c05-9d48-ee775aabfc19",
        "description": "example describing update mock description",
        "private": false,
        "versionTag": "abf07d3d-f8ec-47d4-8015-9fe83078b4ec"
    }
}'
https://api.getpostman.com/mocks/{{mock_uid}}

Postman Collection Item JSON

{
  "name": "Update Mock",
  "request": {
    "method": "PUT",
    "header": [
      {
        "key": "Content-Type",
        "value": "application/json"
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n    \"mock\": {\n        \"name\": \"My Mock Server\",\n        \"environment\": \"11582779-ac1b6608-deb7-4c05-9d48-ee775aabfc19\",\n        \"description\": \"example describing update mock description\",\n        \"private\": false,\n        \"versionTag\": \"abf07d3d-f8ec-47d4-8015-9fe83078b4ec\"\n    }\n}",
      "options": {
        "raw": {
          "language": "json"
        }
      }
    },
    "url": {
      "raw": "https://api.getpostman.com/mocks/{{mock_uid}}",
      "protocol": "https",
      "host": [
        "api",
        "getpostman",
        "com"
      ],
      "path": [
        "mocks",
        "{{mock_uid}}"
      ]
    },
    "description": "This endpoint allows you to update a mock you have created using its `uid`. The endpoint allows you to edit fields,\n* name\n* environment\n* description\n* private\n* versionTag\n\n> Requires <a href=\"#authentication\">API Key</a> as `X-Api-Key` request header or `apikey` URL query parameter."
  },
  "response": [
    {
      "name": "success Response",
      "originalRequest": {
        "method": "PUT",
        "header": [
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n    \"mock\": {\n        \"name\": \"My Mock Server\",\n        \"environment\": \"11582779-ac1b6608-deb7-4c05-9d48-ee775aabfc19\",\n        \"description\": \"example describing update mock description\",\n        \"private\": false,\n        \"versionTag\": \"abf07d3d-f8ec-47d4-8015-9fe83078b4ec\"\n    }\n}",
          "options": {
            "raw": {
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "https://api.getpostman.com/mocks/{{mock_uid}}",
          "protocol": "https",
          "host": [
            "api",
            "getpostman",
            "com"
          ],
          "path": [
            "mocks",
            "{{mock_uid}}"
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"mock\": {\n        \"id\": \"06040138-dd6b-4cce-9a02-7e1c1ab59723\",\n        \"owner\": \"11582779\",\n        \"uid\": \"11582779-06040138-dd6b-4cce-9a02-7e1c1ab59723\",\n        \"collection\": \"11582779-fd6797e2-de6b-4699-975c-85290e4c2499\",\n        \"mockUrl\": \"https://06040138-dd6b-4cce-9a02-7e1c1ab59723.mock.pstmn.io\",\n        \"name\": \"My Mock Server\",\n        \"config\": {\n            \"headers\": [],\n            \"matchBody\": false,\n            \"matchQueryParams\": true,\n            \"matchWildcards\": true\n        },\n        \"environment\": \"11582779-ac1b6608-deb7-4c05-9d48-ee775aabfc19\"\n    }\n}"
    }
  ]
}