Pre-request & Test Scripts

Each request has a Scripts tab with two editors: a pre-request script, run just before the request is sent, and a test script, run after the response arrives. Both are plain JavaScript with access to a small pm object, similar to Postman's sandbox.

pm API

Example: pre-request script

Set a fresh timestamp on every send:

pm.environment.set('timestamp', Date.now());

Example: test script

Assert on the response and pull a value into an environment variable for later requests:

pm.test('status is 200', () => {
  pm.expect(pm.response.status).toBe(200);
});

pm.test('response is not an error', () => {
  pm.expect(pm.response.json()).not.toHaveProperty('error');
});

pm.environment.set('userId', pm.response.json().id);

Test results show up in a Tests tab in the response panel, with a pass/fail count badge. Scripts that throw outside of pm.test() (a syntax error, etc.) show up as a single failed test.