Skip to main content

Script

tip

Pre-request operations are performed before the request is sent, allowing for custom scripts and database connections. These are commonly used for setting input parameters, printing data, etc.

Post-response operations take place after a request is sent and are typically used for setting variables, performing assertions, and similar tasks. They also support custom scripts and database connections.

Add Scripts

To add scripts that execute before the request is sent, use the Pre-request section. Similarly, for actions after the request is sent, use the Post-response section.

image.png

Pre-request Scripts

tip

Pre scripts are scripts that are executed before a request is sent. They are added by adding pre-request and scripts.

Pre-request scripts can perform the following tasks:

  • Writing JS functions for complex calculations.
  • Printing variables.
  • Defining, retrieving, deleting, and clearing environment variables.
  • Defining, retrieving, deleting, and clearing global variables.
  • Retrieving request parameters.
  • Dynamically adding or removing a header request parameter.
  • Dynamically adding or removing a query request parameter.
  • Dynamically adding or removing a body request parameter.
  • Sending HTTP requests.

Writing JS Functions for Complex Calculations

image.png We can define a function _random in the pre-request script,

function _random(){
return 'Hello, World!' + Math.random();
}

This function returns a string: 'Hello, World!' followed by a random number. You can then assign this to a global variable random_var

pm.globals.set("random_var", _random());

Pre Script Printing Debug Variables

We can print the required variables to the console using console.log() to view the current value of a variable. As shown in the example above.

image.png

Defining, Retrieving, Deleting, and Clearing Environment Variables

pm.variables.set("key", "value"); // Set an environment variable with key and value.
pm.variables.get("key"); // Retrieve the value of the environment variable with key.
pm.variables.delete("key"); // Delete the environment variable with key.
pm.variables.clear(); // Clear all defined environment variables.

Defining, Retrieving, Deleting, and Clearing Global Variables

pm.globals.set("key", "value");  // Set a global variable with key and value.
pm.globals.get("key"); // Retrieve the value of the global variable with key.
pm.globals.delete("key"); // Delete the global variable with key.
pm.globals.clear(); // Clear all defined global variables.

Retrieving Request Parameters

You can retrieve request parameters using the request object. For more details, refer to the "EchoAPI Built-in Variables" section.

Dynamically Adding or Removing a Header Request Parameter

pm.setRequestHeader("key", "value"); // Dynamically add a header parameter with key and value.
pm.removeRequestHeader("key"); // Remove the header parameter with key.

Dynamically Adding or Removing a Query Request Parameter

pm.setRequestQuery("key", "value"); // Dynamically add a query parameter with key and value.
pm.removeRequestQuery("key"); // Remove the query parameter with key.

Dynamically Adding or Removing a Body Request Parameter

pm.setRequestBody("key", "value");// Dynamically add a body parameter with key and value.
pm.removeRequestBody("key");// Remove the body parameter with key.

Sending HTTP Requests

You can send an HTTP request through a script:

pm.sendRequest("https://httpbin.org/anything", function (err, response) {
console.log(response.json());
});

Post-response Scripts

tip

Post-response scripts are executed after a request is sent. They are added by including post-execution operations or custom scripts.

Post-response scripts can perform the following tasks:

Post-response scripts can accomplish the following:

  • Writing JS functions for complex calculations.
  • Printing variables.
  • Defining, retrieving, deleting, and clearing environment variables.
  • Defining, retrieving, deleting, and clearing global variables.
  • Retrieving request parameters.
  • Retrieving response parameters.
  • Sending HTTP requests.
  • Testing (asserting) the correctness of the request return results.

The usage is similar to pre-request scripts and follows the same principles.

Pre-request and Post-response Execution Order

image.png