Postman Variables and Scripts

Tired of updating your json payload (attribute) let Postman do it for you with a script and variable

Sometimes you need to generate new timestamps or id’s or … for every new request you send with POSTMAN. The solution is to use pre-request scripts. These scripts are Javascripts that will use macros to populate values in your body content (json in my case).

With the following JSON

And the following pre-request script

then you can get the following result

Inserting a json attribute from one query into another using a script and variable

In my case I was first querying to get a authentication token CSRF_NONCE that I should use in the forthcoming queries as a header.

Step 1) Query for a token

GET https://my.super-secret.server.com/api/security/csrf

Using Basic Authentication (Username/Password)

I would get back a json payload like this

Now let’s create a script that extracts that nonce token

NOTE ! You use “Post-response” script to the left

The script could be something like this

Step 2) Query for the data

Add the header attribute CSRF_NONCE in my case, and set the value to {{NONCE}} which was the variable you set above in the script.

After the query in Step 1) has been executed you can verify that the global variable has received the right value by going to the following view

You can also see the value in the POSTMAN Console

1 thought on “Postman Variables and Scripts

Leave a Reply

Your email address will not be published. Required fields are marked *