You can use pre-request scripts in Postman to execute JavaScript before a request runs. By including code in the Pre-request Script tab for a request, collection, or folder, you can carry out pre-processing such as setting variable values, parameters, headers, and body data. You can also use pre-request scripts for debugging code, for example by logging output to the Postman Console.
An example usage of pre-request scripting could be as follows:
To include code you want to execute before Postman sends a request, do the following:
Select Collections in the sidebar.
Open the request, then select the Pre-request Script tab.
Enter the JavaScript you need to process before the request runs, then select Save.
Select Send to send the request. The code will execute before Postman sends the request to the API.
You can add pre-request scripts to entire collections and folders within collections. In both cases, your pre-request script will run before every request in the collection or direct child request in the folder. This allows you to define commonly used pre-processing or debugging steps you need to execute for multiple requests.
You can define a pre-request script when you first create a collection or folder, or at any time after that.
To add pre-request scripts to a collection or folder, do the following:
Select Collections in the sidebar.
Select the more actions icon , then select Edit.
Select the Pre-request Scripts tab. Enter code that will run before every request in the collection or direct child request in the folder.
Select Save.
After learning the basics of writing pre-request test scripts, you can extend your scripts:
pm
object, visit the Postman JavaScript reference.Last modified: 2022/07/20
Additional resources
Videos
Blog posts
Case Studies