Request creation

I have an environment variable as {‌{url}}. Can I use it inside a script (like pm.sendRequest)?

The following syntax will not work while writing scripts:


You are inside a script, so you need to use the pm.* API to get to that variable. The syntax {‌{url}} works only inside the request builder, not in scripts.


var requestUrl = pm.environment.get("url") + "/mediaitem/");

pm.sendRequest(requestUrl, function (err, res) {
    // do stuff

How to use pre-request script to pass dynamic data in the request body?

In the pre-request script you can simply create a JavaScript object, set the desired values and save it as a variable ()

For example if you want to send a request body that looks like:

    "firstName": "First Name",
    "lastName": "Last Name",
    "email": ""

You can do the following in the pre-request script:

// Define a new object
var user = {
    "firstName": "First Name",
    "lastName": "Last Name",
    "email": ""

// Save the object as a variable.
// JSON.stringify will serialize the object so that Postman can save it
pm.globals.set('user', JSON.stringify(user));

In the request body you can simply use {{user}}. This also works just as well for nested objects:

    "user": {{user}}
    "address": {
        "street": "Foo"
        "number": "2"
        "city": "Bar"

How can I create or modify the request body?

You can use console.log(pm.request.body) to understand the current data structure of the request body. With the method pm.request.body.update you can update the request.

Create/replace JSON request body

const body = {
    mode: "raw",
    raw: JSON.stringify(
            name: "jane",
            age: 33
    options: {
        raw: {
            language: "json"


Add property to JSON request body

const body = JSON.parse(pm.request.body.raw); = '';
pm.request.body.raw = body;

Encode request body as base64


Removing comments from JSON body

const jsonWithComments = pm.request.body.toString();
const jsonWithoutComments = jsonWithComments.replace(/\\"|"(?:\\"|[^"])*"|(\/\/.*|\/\*[\s\S]*?\*\/)/g, (m, g) => g ? "" : m)

// Regex from Stackoverflow:
// & Postman issue tracker (@codenirvana):

const body = {
    mode: "raw",
    raw: jsonWithoutComments,
    options: {
        raw: {
            language: "json"


How can I modify the request headers?

You can modify the request headers from the Pre-request script as follows.

Add header

    key: 'X-Foo',
    value: 'Postman'

Remove header

pm.request.headers.remove('User-Agent'); // may not always work

Update header

        key: "User-Agent",
        value: "Not Postman"


How to generate random data?

Option 1 Using existing Postman random generators

If you need to create an unique string (with every request) and pass it in the request body, in the example below there will be generated an unique GroupName everytime the request is executed.

You can use the variable {‌{$guid}} - this is automatically generated by Postman. Or you can use the current timestamp, {‌{$timestamp}}:

    "Description": "Example_API_Admin-Group_Description"

This will generate something like:

    "Description": "Example_API_Admin-Group_Description"

The disadvantage of this method is that you cannot use these special variables in a pre-request script or test. Additionally they will be only generated once per request, so using {‌{$guid}} more than once will generate the same data in a request.

Option 2 Using existing JavaScript random generators

Below you will find an exemple function that you can use to generate integer number between a specific interval:

function getRandomNumber(minValue, maxValue) {
    return Math.floor(Math.random() * (maxValue - minValue +1)) + minValue;

You can call the function like this:

var myRandomNumber = getRandomNumber(0, 100);

And the output will look similar to:


Below you will find an exemple function that you can use to generate random strings:

function getRandomString() {
    return Math.random().toString(36).substring(2);

You can call the function like this:

var myRandomNumber = getRandomString();

And the output will look similar to:


How to trigger another request from pre-request script?

Option 1 You can trigger another request in the collection from the pre-request script using postman.setNextRequest.

That can be done with:

postman.setNextRequest('Your request name as saved in Postman');

The difficulty is returning to the request that initiated the call. Additionally you need to make sure you do not create endless loops.

Option 2 Another possibility is making an HTTP call from the pre-request script to fetch any data you might need.

Below I am fetching a name from a remote API and setting it as a variable for use in the actual request that will execute right after the pre-request script completed:

var options = { method: 'GET',
  url: ''

pm.sendRequest(options, function (error, response) {
    if (error) throw new Error(error);
    var jsonData = response.json();

Tip You can generate such requests by using the “Code” generator button right below the Save button, once you have a request that works. There you can Select NodeJS > Request and the syntax generated is very similar to what Postman expects.

You can import this example in Postman by using this link:

How to send request with XML body from a script?

You can use the following template to send a XML request from a script. Notice that price is a Postman variable that will be replaced.

const xmlBody = `<?xml version="1.0"?>
<book id="bk101">
    <author>Gambardella, Matthew</author>
    <title>XML Developer's Guide</title>
    <description>An in-depth look at creating applications
    with XML.</description>

const options = {
    'method': 'POST',
    'url': '',
    'header': {
        'Content-Type': 'application/xml'
    body: pm.variables.replaceIn(xmlBody) // replace any Postman variables

pm.sendRequest(options, function (error, response) {
    if (error) throw new Error(error);

How to pass arrays and objects between requests?

Assuming your response is in JSON format, You can extract data from the response by using

var jsonData = pm.response.json();

After this you can set the whole response (or just a subset like this):

pm.environment.set('myData', JSON.stringify(jsonData));

You need to use JSON.stringify() before saving objects / arrays to a Postman variable. Otherwise it may not work (depending on your Postman or Newman version).

In the next request where you want to retrieve the data, just use:

  • {{myData}} if you are inside the request builder
  • var myData = JSON.parse(pm.environment.get('myData'));

Using JSON.stringify and JSON.parse methods is not needed if the values are strings or integers or booleans.

JSON.stringify() converts a value to a JSON string while JSON.parse() method parses a JSON string, creating the value described by the string.

How to read external files?

If you have some information saved on a file locally on your computer, you might want to access this information with Postman.

Unfortunately this is not really possible. There is a way to read a data file in JSON or CSV format, which allows you to make some variables dynamic. These variables are called data variables and are mostly used for testing different iterations on a specific request or collection.

Possible options:

  • start a local server to serve that file and to get it in Postman with a GET request.
  • use Newman as a custom Node.js script and read the file using the filesystem.

How to add a delay between Postman requests?

To add a delay after a request, add the following in your Tests:

setTimeout(() => {}, 10000);

The example above will add a delay of 10000 milliseconds or 10 seconds.