Chilkat Online Tools

delphiAx / Postman API / Update Mock

Back to Collection Items

var
http: TChilkatHttp;
success: Integer;
json: TChilkatJsonObject;
sbRequestBody: TChilkatStringBuilder;
resp: IChilkatHttpResponse;
sbResponseBody: TChilkatStringBuilder;
jResp: TChilkatJsonObject;
respStatusCode: Integer;
Id: WideString;
Owner: WideString;
Uid: WideString;
Collection: WideString;
MockUrl: WideString;
Name: WideString;
MatchBody: Integer;
MatchQueryParams: Integer;
MatchWildcards: Integer;
Environment: WideString;
i: Integer;
count_i: Integer;

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

http := TChilkatHttp.Create(Self);

// 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 := TChilkatJsonObject.Create(Self);
json.UpdateString('mock.name','My Mock Server');
json.UpdateString('mock.environment','11582779-ac1b6608-deb7-4c05-9d48-ee775aabfc19');
json.UpdateString('mock.description','example describing update mock description');
json.UpdateBool('mock.private',0);
json.UpdateString('mock.versionTag','abf07d3d-f8ec-47d4-8015-9fe83078b4ec');

http.SetRequestHeader('Content-Type','application/json');
http.SetRequestHeader('X-API-Key','{{postman_api_key}}');

sbRequestBody := TChilkatStringBuilder.Create(Self);
json.EmitSb(sbRequestBody.ControlInterface);

resp := http.PTextSb('PUT','https://api.getpostman.com/mocks/{{mock_uid}}',sbRequestBody.ControlInterface,'utf-8','application/json',0,0);
if (http.LastMethodSuccess = 0) then
  begin
    Memo1.Lines.Add(http.LastErrorText);
    Exit;
  end;

sbResponseBody := TChilkatStringBuilder.Create(Self);
resp.GetBodySb(sbResponseBody.ControlInterface);

jResp := TChilkatJsonObject.Create(Self);
jResp.LoadSb(sbResponseBody.ControlInterface);
jResp.EmitCompact := 0;

Memo1.Lines.Add('Response Body:');
Memo1.Lines.Add(jResp.Emit());

respStatusCode := resp.StatusCode;
Memo1.Lines.Add('Response Status Code = ' + IntToStr(respStatusCode));
if (respStatusCode >= 400) then
  begin
    Memo1.Lines.Add('Response Header:');
    Memo1.Lines.Add(resp.Header);
    Memo1.Lines.Add('Failed.');

    Exit;
  end;

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

Id := jResp.StringOf('mock.id');
Owner := jResp.StringOf('mock.owner');
Uid := jResp.StringOf('mock.uid');
Collection := jResp.StringOf('mock.collection');
MockUrl := jResp.StringOf('mock.mockUrl');
Name := jResp.StringOf('mock.name');
MatchBody := jResp.BoolOf('mock.config.matchBody');
MatchQueryParams := jResp.BoolOf('mock.config.matchQueryParams');
MatchWildcards := jResp.BoolOf('mock.config.matchWildcards');
Environment := jResp.StringOf('mock.environment');
i := 0;
count_i := jResp.SizeOfArray('mock.config.headers');
while i < count_i do
  begin
    jResp.I := i;
    i := i + 1;
  end;

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