Open In App

How to Write Global Functions in Postman ?

Last Updated : 13 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Postman, a popular API development tool, offers the flexibility to define global functions that can be reused across multiple requests within a collection. These global functions streamline the testing and automation process by allowing users to encapsulate common logic and share it across requests. In this guide, we’ll explore various approaches to writing global functions in Postman, step-by-step.

We will discuss different approaches to writing global functions in Postman.

Using Pre-request Scripts:

  • Pre-request scripts are executed before sending a request to Postman.
  • To define a global function using pre-request scripts, navigate to the pre-request script tab of a request or collection.
  • Write the JavaScript function in the editor provided.

Syntax:

// Define a function in the pre-request script
pm.globals.set("function_name", function() {
// Function logic here
});

Code:

// Define a function to generate authentication token
pm.globals.set("generateToken", function() {
// Logic to generate token
return "generated_token";
});

// Call the function before sending the request
pm.sendRequest({
url: 'https://api.example.com/auth',
method: 'POST',
header: {
'Authorization': pm.globals.get("generateToken")
}
});

Steps to implement Pre-request Scripts:

  1. Open Postman and create a new request or collection.
  2. Navigate to the “Pre-request Script” tab of the request or collection.
  3. Write the JavaScript code to define the global function.
  4. Send the request or run the collection.
  5. Check the request output or console for any logs or results generated by the global function.

imresizer-1709699432217

Leveraging Environment Variables:

  • Environment variables in Postman allow for storing key-value pairs.
  • Define a JavaScript function within the value of an environment variable.
  • Access the function using pm.environment.get(“variable_name”).

Syntax:

// Define a function within an environment variable
pm.environment.set("variable_name", function() {
// Function logic here
});

Code:

// Define a function to calculate a hash value
pm.environment.set("calculateHash", function(data) {
// Logic to calculate hash
return sha256(data);
});

// Access the function from an environment variable
var hashedData = pm.environment.get("calculateHash")("secret_data");
console.log(hashedData);

Steps to implement Environment Variables:

  1. Open Postman and create a new request or collection.
  2. Navigate to the “Variables” tab of the environment associated with the request or collection.
  3. Define an environment variable with the JavaScript function.
  4. Use the function within the request or collection scripts.
  5. Send the request or run the collection.
  6. Check the request output or console for any logs or results generated by the global function.

Utilizing Collection Variables:

  • Similar to environment variables, collection variables store data.
  • Define a JavaScript function within the value of a collection variable.
  • Access the function using pm.collectionVariables.get(“variable_name”).

Syntax:

// Define a function within a collection variable
pm.collectionVariables.set("variable_name", function() {
// Function logic here
});

Code:

// Define a function to format date
pm.collectionVariables.set("formatDate", function(date) {
// Logic to format date
return moment(date).format("YYYY-MM-DD");
});

// Access the function from a collection variable
var formattedDate = pm.collectionVariables.get("formatDate")(new Date());
console.log(formattedDate);

Steps to implement Collection Varibles:

  1. Open Postman and create a new request or collection.
  2. Navigate to the “Variables” tab of the collection.
  3. Define a collection variable with the JavaScript function.
  4. Use the function within the request or collection scripts.
  5. Send the request or run the collection.
  6. Check the request output or console for any logs or results generated by the global function.

Defining Functions in Collection Scripts:

  • Collection scripts are executed before or after the entire collection run.
  • Define global functions in the collection scripts tab.
  • Access the function directly from any request within the collection.

Syntax:

// Define a function in the collection script
function functionName() {
// Function logic here
}

Code:

// Define a function to log request details
function logRequestDetails(request) {
console.log("Request URL:", request.url);
console.log("Request Method:", request.method);
}

// Access the function from any request within the collection
logRequestDetails(pm.request);

Steps to implement Collection Scripts:

  1. Open Postman and create a new request or collection.
  2. Navigate to the “Pre-request Script” or “Tests” tab of the collection.
  3. Define the JavaScript function within the collection script.
  4. Use the function within any request or test scripts within the collection.
  5. Send the request or run the collection.
  6. Check the request output or console for any logs or results generated by the global function.

Conclusion

Writing global functions in Postman enhances productivity by reducing redundancy and promoting code reusability. By leveraging pre-request scripts, environment variables, collection variables, or collection scripts, developers can encapsulate common logic and share it across requests effectively. With this guide, you can harness the full power of Postman’s capabilities for API testing and automation.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads