What is Pre-Request Script in Postman?


The Pre-Request Script is used to run a JavaScript prior to the execution of a request. By incorporating a Pre-Request Script for a Collection, request or a folder, we can execute precondition steps like defining a variable, Parameters, Headers, Response, or logging console output.

We can include a Pre-Request Script to set the order of execution of requests within a Collection. A Pre-Request Script can also handle a scenario in which a value yielded from the request one has to be fed to the next request or the value yielded from the request one has to be processed before moving to the next request.

A Pre-Request can also be utilized to set a value from the Response field to a variable linked to a script in the Tests tab. These scripts are defined in the Pre-request Script tab in Postman.

Add the below JavaScript in the Pre-Request Script tab −

console.log("This is a Postman Tutorial in Tutorialspoint")

Select a GET request and enter an endpoint then click on Send.

Postman Console Output:

Pre-Request Script to Collections

Step1 − Click on the three dots beside a Collection in the sidebar. Then click on Edit.

Step2 − EDIT COLLECTION pop-up comes up. Click on the Pre-Request Scripts tab to add a pre-condition script. Then click on Update.

Updated on: 25-Jun-2021

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements