Skip to content

How To Use Environment Variable In Request Body In Postman

API Testing Blog

Utilizing Environment Variables in Postman Request Body for API Testing

Postman is a powerful tool for API testing, and one of its key features is the ability to utilize environment variables to manage test data effectively. This guide explores different ways to integrate environment variables within your Postman requests, making your API testing process more flexible and efficient.

1. Defining Environment Variables:

Before you can use environment variables in your request body, you first need to define them.

Steps:

  1. Open Postman: Navigate to the Postman app or website.
  2. Access Environment Variables: Click on the “Environments” tab located in the left sidebar.
  3. Create a New Environment: In the “Environments” section, click “Add” and give your environment a descriptive name (e.g., “Test Environment”).
  4. Add Variables: Click on the “Variables” tab within the environment you just created. Now you can add individual variables by clicking “Add” and providing a variable name (e.g., “username”, “password”) and its corresponding value.

Example:

Variable NameValue
usernametestuser
passwordtestpassword

2. Using Environment Variables in Request Body:

Once you have defined your variables, you can use them directly within your Postman request bodies.

Steps:

  1. Compose your Request: Open a new request in Postman.
  2. Select the Environment: In the “Environment” dropdown located in the top right corner, choose the environment where your variables are defined.
  3. Insert Variables: Within the body of your request, use the syntax {{variable_name}} to reference the environment variable you want to use.

Example:

Request Body (JSON):

{
"username": "{{username}}",
"password": "{{password}}"
}

This request will substitute the values of the username and password variables defined in your environment into the request body during execution.

3. Using Environment Variables with Dynamic Values:

For cases where you need to use environment variables dynamically, you can leverage Postman’s scripting capabilities.

Steps:

  1. Create a Pre-request Script: In the “Pre-request Script” tab of your request, access the scripting environment.
  2. Define a Variable: Declare a variable using the pm.environment.set() function to assign a dynamic value, possibly generated using other functions, to an environment variable.

Example:

// Generate a random number
const randomNumber = Math.floor(Math.random() * 100);
// Set the generated number as an environment variable
pm.environment.set("random_value", randomNumber);

Request Body (JSON):

{
"random_number": "{{random_value}}"
}

This example will generate a random number and assign it to the environment variable random_value. Subsequently, the random_value will be included in the request body.

4. Using Environment Variables for Data-Driven Testing:

Environment variables are instrumental in data-driven testing, allowing you to execute the same test with different data sets.

Steps:

  1. Define Multiple Values: Set up a collection of datasets for your test by defining different environment variables with associated values for each data set.
  2. Run Tests with Different Environments: Utilize Postman’s “Collections” and “Environments” functionality to run your tests with different environments, each containing distinct sets of environment variable values.

Example:

Environment 1 (User 1):

Variable NameValue
usernameuser1@example.com
passwordpassword123

Environment 2 (User 2):

Variable NameValue
usernameuser2@example.com
passwordtestpass

Request Body (JSON):

{
"username": "{{username}}",
"password": "{{password}}"
}

By switching between environments, you can test the same API endpoint with different user credentials ensuring your API works correctly for various data inputs.

5. Using Environment Variables with Collections:

For more organized testing, you can leverage environment variables within Postman Collections.

Steps:

  1. Create a Collection: In Postman, create a new collection to organize your API requests.
  2. Define Collection Variables: Click on the “Variables” tab within your newly created collection to define variables that are globally accessible within the collection.
  3. Use Collection Variables in Requests: Use the same syntax {{variable_name}} to access the collection variables within your collection requests.

Example:

Collection Variable:

Variable NameValue
API_BASE_URLhttps://api.example.com/

Request URL:

{{API_BASE_URL}}/users

With this structure, you can change the API base URL once for the entire collection, making adjustments and updates easier.

By understanding how to effectively utilize environment variables in Postman, you can significantly enhance the robustness and flexibility of your API testing process. From managing test data to enabling data-driven testing and organizing your API requests within Collections, environment variables become a vital tool in your API testing arsenal. Remember to explore different scenarios and experiment with these techniques to find the most efficient approach that aligns with your specific API testing requirements.

API Testing Blog