Files
oam/knowledge base/postman.md
2023-11-19 16:22:59 +01:00

2.6 KiB

Postman

API platform for building and using APIs.

Table of contents

  1. TL;DR
  2. Scripting
  3. Further readings
  4. Sources

TL;DR

Export environments:

  1. Select Environments in the sidebar.
  2. Select the one environment to export.
  3. In the workbench, select the more actions icon (the three dots).
  4. In the more actions menu, select Export.
  5. Choose a path and name to download a newly generated JSON file containing the chosen environment definition.

Export or update variables from responses using those responses' test scripts:

const responseJson = pm.response.json();
pm.environment.set("access_token",responseJson.access_token);

Scripting

You can execute JavaScript code:

  • Before a request is sent to the server, as a pre-request script under the Pre-request Script tab.
  • After a response is received, as a test script under the Tests tab.

Basic examples:

// Expect a specific status code.
pm.test("Response status code is 200", function () {
  pm.response.to.have.status(200);
});

// Expect specific status codes.
pm.test("Response status code is 200 or 201", () => {
  pm.expect(pm.response.code).to.be.oneOf([200,201]);
});

pm.test("Multiple assertions", () => {
  const responseJson = pm.response.json();
  pm.expect(responseJson.type).to.eql('vip');
  pm.expect(responseJson.name).to.be.a('string');
  pm.expect(responseJson.id).to.have.lengthOf('1');
});

// Export values from responses.
const responseJson = pm.response.json();
pm.environment.set("access_token",responseJson.access_token);
pm.globals.set("access_token",responseJson.access_token);
pm.collectionVariables.set("access_token",responseJson.access_token);

More examples here.

Further readings

Sources

All the references in the [further readings] section, plus the following: