Skip to content

How To Use Global Variable In Postman Request Body

API Testing Blog

How to Use Global Variables in Postman Request Body

Global variables are a powerful tool in Postman for managing and reusing data across multiple requests. This can be particularly useful when testing APIs, especially when you need to share values between different API calls. Here’s a comprehensive guide on using global variables in Postman request bodies:

1. Defining Global Variables

  • Navigate to “Globals”: Click on the “Globals” tab in the left-hand sidebar of Postman.
  • Add a New Variable: Click on the “Add Variable” button and enter a name for your variable (e.g., “apiKey”).
  • Set the Value: Enter the desired value for your variable (e.g., “your_api_key”).

2. Using Global Variables in Request Bodies

  • Use Double Braces: In your request body, enclose the global variable name within double curly braces ({{ }}) to use its value.

Example:

{
"apiKey": "{{apiKey}}",
"email": "test@example.com",
"password": "your_password"
}

This will substitute the actual key value stored in the apiKey global variable within the JSON request body.

3. Passing Variables to Other Requests

  • Use the “Response Body” of one request in another.
  • “Set a Global Variable” for a request: This is an option that can be found in the “Tests” tab of a request.

Example: We can extract an id from a successful API response and store it in a global variable to use later.

Step 1: In the Tests tab of the first request, add the following code to store the id from the response body:

pm.test("Extract the ID from the response", function () {
var jsonData = pm.response.json();
pm.globals.set("userId", jsonData.id);
});

Step 2: In the “Send” tab of the next request, use the global variable ({{userId}}) in your request body:

{
"userId": "{{userId}}",
"message": "Hello from Postman"
}

This example demonstrates how to use a global variable to pass values between requests, making your API testing workflow more efficient.

4. Accessing Global Variables in Tests

You can also access global variable values directly in your Postman tests using the pm.globals.get() function.

Example:

pm.test("Verify API Key", function () {
var apiKey = pm.globals.get("apiKey");
pm.expect(apiKey).to.be.equal("your_api_key");
});

This tests checks if the value of the apiKey global variable matches the expected value.

5. Reusing Global Variables Across Collections

Global variables are accessible across all your Postman Collections. This allows you to manage and reuse variables among multiple tests for different APIs, making your testing process more organized and consistent.

6. Using Environment Variables for Dynamic Configuration

While global variables serve many purposes, you might also consider using environment variables for managing dynamic settings, such as different API endpoint URLs for testing on different environments (e.g., development, staging, production).

  • Define Environment Variables: Go to the “Environments” tab, select an environment, and add variables.
  • Access Environment Variables in Requests: Use double curly braces to access environment variables in your request bodies similar to how you use global variables.

Example:

{
"url": "{{baseUrl}}/api/users",
"apiKey": "{{apiKey}}",
"email": "test@example.com",
"password": "your_password"
}

Here, baseUrl and apiKey are environment variables, allowing you to easily switch environments by selecting different environment configurations within Postman.

By utilizing global and environment variables, you can significantly streamline your API testing workflow, making it more efficient, adaptable, and organized. Remember to choose the right type of variable based on your specific needs and testing context.

API Testing Blog