Skip to content

How To Use Variable In Postman Body

API Testing Blog

How to Use Variables in Postman Body for Effective API Testing

Variables are a powerful tool in Postman for making your API testing more efficient and flexible. By using variables, you can create dynamic requests that adapt to different scenarios, avoid repetitive data entry, and enhance reusability.

Understanding Postman Variables

Postman variables are placeholders that hold values that can be used throughout your requests, collections, and environments. They allow you to:

  • Parameterize requests: Replace static values with variables to create dynamic requests.
  • Manage data: Store shared data like authentication tokens, API keys, or test data in variables.
  • Organize testing: Group related variables into environments for different testing scenarios.

Defining and Using Variables in the Postman Body

1. Defining Variables

You can define variables in several ways:

  • Environment Variables: These are global variables accessible across all requests within a specific environment.

    • Go to Environments Tab: Click on the “Environments” tab in the Postman sidebar.
    • Create a New Environment: Click the “Add” button to create a new environment.
    • Define Variables: Add a variable name and its value in the table.

    Example:

    Variable Name: apiKey
    Value: your_api_key_value
  • Collection Variables: These variables are specific to a particular collection and are accessible by all requests within that collection.

    • Go to Collection Tab: Click on the “Collections” tab in the Postman sidebar.
    • Edit a Collection: Click on the three dots next to your collection and select “Edit”.
    • Add Variables: Click the “Variables” tab and add new variables and their values.

    Example:

    Variable Name: baseUrl
    Value: https://api.example.com/v1
  • Global Variables: These variables are accessible from anywhere within Postman.

    • Access Settings: Click on the “Settings” icon in the top right corner.
    • Manage Globals: Go to the “Globals” tab.
    • Add Variables: Add a variable name and its value.

    Example:

    Variable Name: globalToken
    Value: your_global_token

2. Using Variables in the Postman Body

Once you’ve defined your variables, you can use them in the request body. Here are two ways to do this:

  • Directly in the Body: Replace static values in your body with the variable name surrounded by double curly braces ({{ }}).

    Example:

    {
    "name": "{{username}}",
    "email": "{{userEmail}}",
    "api_key": "{{apiKey}}"
    }
  • Using Pre-request Scripts: You can dynamically generate or update the body content using pre-request scripts.

    Example:

    pm.variables.set("bodyContent", JSON.stringify({
    "name": "John Doe",
    "email": "john.doe@example.com",
    "api_key": pm.environment.get("apiKey")
    }));

    This script sets a new variable bodyContent with the JSON body, dynamically incorporating the values from variables name, email, and the environment variable apiKey.

Practical Examples: How to Use Variables in Postman Body

Using Environment Variables in a POST Request

  1. Create a new environment: Define an environment named “TestEnv” with the following variables:
    • baseUrl: https://api.example.com/v1
    • token: your_token_value
  2. Create a new POST request: Add a new request to your collection and select the “TestEnv” environment.
  3. Construct the body:
    {
    "name": "John Doe",
    "email": "john.doe@example.com",
    "token": "{{token}}"
    }
  4. Send the request: Send the request and verify that the token value from the environment is correctly sent in the body.

Using Collection Variables in a GET Request

  1. Define a collection variable: Create a collection variable named userId in your collection with a value like “12345”.
  2. Create a new GET request: Add a new request to your collection.
  3. Use the variable in the url:
    {{baseUrl}}/users/{{userId}}
  4. Send the request: Observe that the request URL dynamically includes the userId value from the collection variable.

Best Practices for Using Variables in Postman Body

  • Keep variables organized: Define variables in the appropriate scope (environment, collection, or global).
  • Use descriptive variable names: This makes managing and understanding your tests easier.
  • Avoid hardcoded values: Use variables whenever possible.
  • Document variable usage: Add comments or descriptions to your variables for better clarity.

By leveraging Postman variables effectively, you can create more dynamic and reusable API tests, making your testing workflow more efficient and robust.

API Testing Blog