Write pre-request scripts to add dynamic behavior in Postman | Postman Learning Center (2024)

You can use pre-request scripts in Postman to execute JavaScript before a request runs. By including code in the Pre-request 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.

Pre-request scripting example

The following is an example of using pre-request scripts:

  • You have a series of requests in a collection and are running them in a sequence, such as when using the Collection Runner.
  • The second request is dependent on a value returned from the first request.
  • The value needs to be processed before you pass it to the second request.
  • The first request sets the data value from a response field to a variable in its post-response script.
  • The second request retrieves the value and processes it in its pre-request script, then sets the processed value to a variable (which is referenced in the second request, for example, in its parameters).

Scripting before your request runs

To include code you want to execute before Postman sends a request, do the following:

  1. Select Collections in the sidebar.

  2. Open the request, then select the Scripts tab.

  3. Select the Pre-request tab.

  4. Enter the JavaScript you need to process before the request runs, then select Write pre-request scripts to add dynamic behavior in Postman | Postman Learning Center (1) Save.

  5. Select Send to send the request. The code will execute before Postman sends the request to the API.

    Write pre-request scripts to add dynamic behavior in Postman | Postman Learning Center (2)

Add documentation to pre-request scripts

Postman supports JSDoc for documenting JavaScript functions in your pre-request scripts. Documentation added to your functions using JSDoc will display in a popup window when you call your functions. You can use the official JSDoc documentation to learn how to add documentation to your pre-request scripts.

The following example has documentation for the logger function using JSDoc. The documentation explains what the function does, and defines what the data parameter is used for and that it accepts a string data type.

/** * This function prints a string to the Postman Console. * @param {string} data - The text to print to the Postman Console. */function logger (data) { console.log(`Logging information to the console, ${data}`)}

Reuse pre-request scripts

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 enables 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.

You can also store pre-request scripts in the Package Library. This enables you to maintain commonly used scripts in a single location, share them with your team, and reuse them in your workspaces.

To add pre-request scripts to a collection or folder, do the following:

  1. Select Collections in the sidebar.
  2. Select a collection or folder.
  3. Select the Scripts tab.
  4. Select the Pre-request tab. Enter code that will run before every request in the collection or direct child request in the folder.
  5. Select Write pre-request scripts to add dynamic behavior in Postman | Postman Learning Center (3) Save.

Next steps

After learning the basics of writing pre-request scripts, you can extend your scripts:

Write pre-request scripts to add dynamic behavior in Postman | Postman Learning Center (2024)
Top Articles
If You Invested $1000 in Netflix a Decade Ago, This is How Much It'd Be Worth Now
Guide to the top taxi apps in Innsbruck - Christian Transfers Blog
Directions To Franklin Mills Mall
Chase Bank Operating Hours
Northern Whooping Crane Festival highlights conservation and collaboration in Fort Smith, N.W.T. | CBC News
50 Meowbahh Fun Facts: Net Worth, Age, Birthday, Face Reveal, YouTube Earnings, Girlfriend, Doxxed, Discord, Fanart, TikTok, Instagram, Etc
Wild Smile Stapleton
Matthew Rotuno Johnson
Tight Tiny Teen Scouts 5
Red Heeler Dog Breed Info, Pictures, Facts, Puppy Price & FAQs
83600 Block Of 11Th Street East Palmdale Ca
Aces Fmc Charting
OpenXR support for IL-2 and DCS for Windows Mixed Reality VR headsets
Springfield Mo Craiglist
Google Flights Missoula
Pizza Hut In Dinuba
Paychex Pricing And Fees (2024 Guide)
Byui Calendar Fall 2023
Jalapeno Grill Ponca City Menu
H12 Weidian
Used Safari Condo Alto R1723 For Sale
2013 Ford Fusion Serpentine Belt Diagram
Garnish For Shrimp Taco Nyt
Conscious Cloud Dispensary Photos
Shadbase Get Out Of Jail
Bòlèt Florida Midi 30
Wonder Film Wiki
Hobby Lobby Hours Parkersburg Wv
Southtown 101 Menu
Lininii
Poe T4 Aisling
South Florida residents must earn more than $100,000 to avoid being 'rent burdened'
Www.craigslist.com Syracuse Ny
Everstart Jump Starter Manual Pdf
Myhrconnect Kp
Moxfield Deck Builder
Pitco Foods San Leandro
Truckers Report Forums
October 31St Weather
Aliciabibs
About :: Town Of Saugerties
Bernie Platt, former Cherry Hill mayor and funeral home magnate, has died at 90
Weather Underground Bonita Springs
Wait List Texas Roadhouse
Man Stuff Idaho
Bekah Birdsall Measurements
The Average Amount of Calories in a Poke Bowl | Grubby's Poke
Nope 123Movies Full
Gear Bicycle Sales Butler Pa
Diccionario De Los Sueños Misabueso
Parks And Rec Fantasy Football Names
Heisenberg Breaking Bad Wiki
Latest Posts
Article information

Author: Fredrick Kertzmann

Last Updated:

Views: 5677

Rating: 4.6 / 5 (46 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Fredrick Kertzmann

Birthday: 2000-04-29

Address: Apt. 203 613 Huels Gateway, Ralphtown, LA 40204

Phone: +2135150832870

Job: Regional Design Producer

Hobby: Nordic skating, Lacemaking, Mountain biking, Rowing, Gardening, Water sports, role-playing games

Introduction: My name is Fredrick Kertzmann, I am a gleaming, encouraging, inexpensive, thankful, tender, quaint, precious person who loves writing and wants to share my knowledge and understanding with you.