💡 If you like this website, please share it with your friends and network! 🚀
Back to All Questions
Question 41 of 100
Intermediate API Testing
Intermediate

Q41: What is a Pre-request Script in Postman? Core Use Cases.

📝Core Concept

What is a Pre-request Script in Postman? Core Use Cases.

Key Takeaways & Architecture Summary

  • Executes JavaScript code in the sandbox environment BEFORE the request is sent.
  • Ideal for dynamically generating signatures, timestamps, or tokens.
  • Used to clean up variables or set request body configurations.

Direct Answer Summary

A Pre-request Script is a JavaScript block executed in Postman's sandbox before the request is dispatched. Typical use cases include generating dynamic payloads, calculating cryptographic HMAC signatures, fetching dynamic access tokens, or formatting dates to inject into header variables.

⚠️ Senior Engineering Warning (Red Flag)

Never place resource assertions inside Pre-request scripts. The request has not been sent yet; attempting to read response headers or payloads in a pre-request script will crash the test.

💡 STAR Architectural Explanation & Pro Tip

Pre-request scripts run in a secure JavaScript sandbox, allowing you to manipulate strings, hash payloads using CryptoJS, and manage active session configurations.

RestAssuredTest.java
Rest-Assured + Java
// Pre-request script: Dynamic date formatting and header injection
const dateHeader = new Date().toUTCString();
pm.environment.set("currentDate", dateHeader);

// This variable is now ready to be injected into the request header as {{currentDate}}