How to use Global Variable in Postman Request?

PostmanREST APISOAP API

We can use a Global variable in Postman Request. . We can set, get and clear a Global variable at runtime using the scripts. This is achieved by the pm.* function. The script to use a Global variable can be included either in the Tests or Pre-Request Script tab

To set a Global variable, the script should be −

pm.globals.set('<name of Global variable>', '<value of variable>')

To get the value of a Global variable, the script should be −

pm.globals.get('<name of Global variable>')

To get the value of the Global variable in the Postman console, the script should be −

console.log(pm.globals.get('<name of Global variable>')

To delete a Global variable, the script is −

pm.globals.unset('<name of Global variable>')

Let us try to work with a Global variable g.

Step1 − Add the below script to set the value value1 for the Global variable g.

pm.globals.set('g', 'value1')

Step2 − Add the below script to get the value of Global variable g and print it in console.

console.log(pm.globals.get('g'))

Step3 − Add the below script to delete the value of Global variable g and verify it.

pm.globals.unset('g'))
console.log(pm.globals.get('g'))

Step4 − Click on Send to execute a request.

Step5 − After the Response is received, open the Postman Console. It first displays value1 which is the value set for the Global variable g. Next, it displays undefined since the value of the Global variable g has been deleted in the Step3.

raja
Published on 25-Jun-2021 15:15:26
Advertisements