Postman Sandbox API reference

Note: The functionality described here is exclusive to Postman’s native apps for Mac, Windows, and Linux.

Global functions (pm.*)

require

require(moduleName:String):function → *

The require function allows you to use the sandbox built-in library modules. The list of available libraries are listed below. The list links to their corresponding documentation.

  1. atob → v2.0.3
  2. btoa → v1.1.2
  3. chai → v3.5.0
  4. cheerio → v0.22.0
  5. crypto-js → v3.1.9-1
  6. csv-parse/lib/sync → 1.2.1
  7. lodash → v4.17.2 (when used with require, the inbuilt _ object is for v3.10.1)
  8. moment → v2.18.1 (sans locales)
  9. postman-collection → v1.2.0
  10. tv4 → v1.2.7
  11. uuid → (the module loaded is a shim for original module)
  12. xml2js → 0.4.19

A number of NodeJS modules are also available:

  1. path
  2. assert
  3. buffer
  4. util
  5. url
  6. punycode
  7. querystring
  8. string_decoder
  9. stream
  10. timers
  11. events

In order to use a library, simply call the require function and pass the module name as a parameter and assign the return of the function to a variable.

var atob = require('atob'),
    _ = require('lodash'), 

    arrayOfStrings,
    base64Strings;

arrayOfStrings =  = ['string1', 'string2'];

base64Strings = _.map(arrayOfStrings, atob); 

console.log(base64Strings);

pm

pm:Object

The pm object encloses all information pertaining to the script being executed and allows one to access a copy of the request being sent or the response received. It also allows one to get and set environment and global variables.

pm.info:Object

The pm.info object contains information pertaining to the script being executed. Useful information such as the request name, request Id, and iteration count are stored inside of this object.

  • pm.info.eventName:String Contains information whether the script being executed is a “prerequest” or a “test” script.
  • pm.info.iteration:Number Is the value of the current iteration being run.
  • pm.info.iterationCount:Number Is the total number of iterations that are scheduled to run.
  • pm.info.requestName:String
  • pm.info.requestId:String

pm.sendRequest

pm.sendRequest:Function

The pm.sendRequest function allows sending HTTP/HTTPS requests asynchronously. Simply put, with asynchronous scripts, you can now execute logic in the background if you have a heavy computational task or are sending multiple requests. Instead of waiting for a call to complete and blocking any next requests, you can designate a callback function and be notified when the underlying operation has finished.

Some things to know about pm.sendRequest():

  • The method accepts a collection SDK compliant request and a callback. The callback receives 2 arguments, an error (if any) and SDK compliant response.
  • It can be used in the pre-request or the test script.
// example with a plain string URL
pm.sendRequest('https://postman-echo.com/get', function (err, res) {
    if (err) {
        console.log(err);
    } else {
        pm.environment.set("variable_key", "new_value");
    }
});

// Example with a full fledged SDK Request
const echoPostRequest = {
  url: 'https://postman-echo.com/post',
  method: 'POST',
  header: 'headername1:value1',
  body: {
    mode: 'raw',
    raw: JSON.stringify({ key: 'this is json' })
  }
};
pm.sendRequest(echoPostRequest, function (err, res) {
  console.log(err ? err : res.json());
});

// example containing a test ** under the Tests tab only
pm.sendRequest('https://postman-echo.com/get', function (err, res) {
  if (err) { console.log(err); }
  pm.test('response should be okay to process', function () {
    pm.expect(err).to.equal(null);
    pm.expect(res).to.have.property('code', 200);
    pm.expect(res).to.have.property('status', 'OK');
  });
});

Extended Reference:

pm.globals

pm.globals:VariableScope

  • pm.globals.has(variableName:String):function → Boolean
  • pm.globals.get(variableName:String):function → *
  • pm.globals.set(variableName:String, variableValue:String):function
  • pm.globals.unset(variableName:String):function
  • pm.globals.clear():function
  • pm.globals.toObject():function → Object

pm.environment

s", sans-serif; font-weight: 600; line-height: 50px; color: rgb(40, 40, 40); margin-top: 20px; margin-bottom: 10px; font-size: 30px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">Postman Sandbox API referenceNote: The functionality described here is exclusive to Postman’s native apps for Mac, Windows, and Linux.

Global functions (pm.*)

require

require(moduleName:String):function → *

The require function allows you to use the sandbox built-in library modules. The list of available libraries are listed below. The list links to their corresponding documentation.

  1. atob → v2.0.3
  2. btoa → v1.1.2
  3. chai → v3.5.0
  4. cheerio → v0.22.0
  5. crypto-js → v3.1.9-1
  6. csv-parse/lib/sync → 1.2.1
  7. lodash → v4.17.2 (when used with require, the inbuilt _ object is for v3.10.1)
  8. moment → v2.18.1 (sans locales)
  9. postman-collection → v1.2.0
  10. tv4 → v1.2.7
  11. uuid → (the module loaded is a shim for original module)
  12. xml2js → 0.4.19

A number of NodeJS modules are also available:

  1. path
  2. assert
  3. buffer
  4. util
  5. url
  6. punycode
  7. querystring
  8. string_decoder
  9. stream
  10. timers
  11. events

In order to use a library, simply call the require function and pass the module name as a parameter and assign the return of the function to a variable.

var atob = require('atob'),
    _ = require('lodash'), 

    arrayOfStrings,
    base64Strings;

arrayOfStrings =  = ['string1', 'string2'];

base64Strings = _.map(arrayOfStrings, atob); 

console.log(base64Strings);

pm

pm:Object

The pm object encloses all information pertaining to the script being executed and allows one to access a copy of the request being sent or the response received. It also allows one to get and set environment and global variables.

pm.info:Object

The pm.info object contains information pertaining to the script being executed. Useful information such as the request name, request Id, and iteration count are stored inside of this object.

  • pm.info.eventName:String Contains information whether the script being executed is a “prerequest” or a “test” script.
  • pm.info.iteration:Number Is the value of the current iteration being run.
  • pm.info.iterationCount:Number Is the total number of iterations that are scheduled to run.
  • pm.info.requestName:String
  • pm.info.requestId:String

pm.sendRequest

pm.sendRequest:Function

The pm.sendRequest function allows sending HTTP/HTTPS requests asynchronously. Simply put, with asynchronous scripts, you can now execute logic in the background if you have a heavy computational task or are sending multiple requests. Instead of waiting for a call to complete and blocking any next requests, you can designate a callback function and be notified when the underlying operation has finished.

Some things to know about pm.sendRequest():

  • The method accepts a collection SDK compliant request and a callback. The callback receives 2 arguments, an error (if any) and SDK compliant response.
  • It can be used in the pre-request or the test script.
// example with a plain string URL
pm.sendRequest('https://postman-echo.com/get', function (err, res) {
    if (err) {
        console.log(err);
    } else {
        pm.environment.set("variable_key", "new_value");
    }
});

// Example with a full fledged SDK Request
const echoPostRequest = {
  url: 'https://postman-echo.com/post',
  method: 'POST',
  header: 'headername1:value1',
  body: {
    mode: 'raw',
    raw: JSON.stringify({ key: 'this is json' })
  }
};
pm.sendRequest(echoPostRequest, function (err, res) {
  console.log(err ? err : res.json());
});

// example containing a test ** under the Tests tab only
pm.sendRequest('https://postman-echo.com/get', function (err, res) {
  if (err) { console.log(err); }
  pm.test('response should be okay to process', function () {
    pm.expect(err).to.equal(null);
    pm.expect(res).to.have.property('code', 200);
    pm.expect(res).to.have.property('status', 'OK');
  });
});

Extended Reference:

pm.globals

pm.globals:VariableScope

  • pm.globals.has(variableName:String):function → Boolean
  • pm.globals.get(variableName:String):function → *
  • pm.globals.set(variableName:String, variableValue:String):function
  • pm.globals.unset(variableName:String):function
  • pm.globals.clear():function
  • pm.globals.toObject():function → Object

pm.environment

results matching ""

    No results matching ""