Creating Variables using Pre Request Script using Postman?

PostmanREST APISOAP API

Postman

12 Lectures 1 hours

Postman Rest API Testing 2022 Complete Guide

25 Lectures 2 hours

Software Testing - Selenium, Postman And More By Spotle.ai

18 Lectures 2.5 hours

We can create variables using Pre-Request Script in Postman. We can set, update, or clear variables prior to the execution of the actual request. To set an Environment variable, we have to add the below script under the Pre-Request Script tab.

postman.setEnvironmentVariable(name of variable, value of variable)

To set a Global variable, we have to add the below script under the Pre-Request Script tab.

postman.setGlobalVariable(name of variable, value of variable)

Step1 − Select an Environment – Environment_Test from the No Environment dropdown.

Step2 − In the address bar enter {{URL}} (an Environment variable available to the Environment_Test). Hover over {{URL}} to find the Scope of the variable as Environment. Also, it is represented as E.

Step3 − Navigate to the Pre-Request Script tab and add the below script −

postman.setEnvironmentVariable('tutorial','postman')

This shall create a variable within the Environment_Test Environment having the name tutorial and value postman.

Step4 − Click on the Send button and search for the tutorial variable by clicking the eye icon.

Updated on 25-Jun-2021 16:14:26