Pre-request Scripts

Pre-request scripts are a piece of code that will run before the execution of a request. It runs in the Postman sandbox and allows us to add dynamic behavior to request execution.

We can apply the pre-request scripts at a request level, a collection level, or a folder level, which means that a pre-request script will apply to all the requests that are part of that collection or folder.

However, if we have defined scripts in all three means in the request, collection and folder, then the script must run in a given order:

  • A pre-request script associated with a collection will run before any request in the collection.
  • A pre-request script of a folder will run before any request in the folder.

We can use the pre-request script for a pre-processing task like setting parameters, variable values, body data, and headers. We may also use pre-request scripts to debug the code, such as by logging output to the console. Also, using the concept of the pre-request script, we can get the output of the function such as date, time, timestamp, etc.

Let's look at a simple example to prove that pre-request scripts are running before a request is executed:

  • Click on the postman console. Postman console stores all the things that happen in the request. Through the postman console, we can see the logs and errors. It is like browsers development console in which all the things are visible, including whole requests that users have sent to that website or to the page code.
  • To open the postman console, select the "Postman Console" icon from the bottom of the window or press ctrl+alt+c.
Pre-request Scripts

Clear the old logs from the console.

Pre-request Scripts
  • Now, enter the URL in the URL text field.
Pre-request Scripts
  • Go to the Pre-request Script tab and write the following script:

Pre-request Scripts
  • Press the Send button and check the postman console.
Pre-request Scripts

Here, you can see that the pre-request script is running prior to the request execution.

Creating Variables by Pre-request Script

Here, we will use the environment variable.

  • Go to Environment quick look button visible as an eye icon available in the top right corner of the builder section.
Pre-request Scripts
  • Select your environment from the drop-down. Here we will use Development API that we have already created while learning about 'Variables in Postman'.
Pre-request Scripts
  • In the address bar enter the following url: {{url}}/utilities/weatherfull/city/Bengaluru
Pre-request Scripts
  • Go to the Pre-request Script Write the following code inside the editor:

Pre-request Scripts

This will create a variable inside the "Development" environment with the name "username" and value "Nikita".

  • Select the Send button and look at the current variable by selecting the Environment quick look button visible as an eye icon.
Pre-request Scripts

Here, we can see the created "username" variable available in the environment.

Similarly, add the following script in the pre-request script editor:


Pre-request Scripts

Again check the Development environment variable:

Pre-request Scripts

Hence, we can perform multiple tasks in the pre-request script before script execution without going through the process of creating and deleting variables again and again.


Next TopicTest Scripts




Latest Courses