0
0
Postmantesting~5 mins

Dynamic URL building in Postman

Choose your learning style9 modes available
Introduction

Dynamic URL building helps you create web addresses that change based on your test data. This makes your tests flexible and reusable.

When you need to test an API with different user IDs in the URL.
When the URL has query parameters that change with each test.
When you want to run the same test on different environments like dev, test, and prod.
When you want to pass variables like dates or tokens inside the URL.
When automating tests that require URLs to update based on previous responses.
Syntax
Postman
https://{{baseUrl}}/users/{{userId}}?date={{currentDate}}
Use double curly braces {{}} to insert variables in Postman URLs.
Variables can come from environment, collection, or global scopes.
Examples
This URL uses variables for base URL and product ID to test different products.
Postman
https://{{baseUrl}}/products/{{productId}}
Here, query parameters change dynamically with search term and page number.
Postman
https://api.example.com/search?query={{searchTerm}}&page={{pageNumber}}
This URL changes the environment (like dev or prod) and data ID dynamically.
Postman
https://{{env}}.example.com/data/{{dataId}}
Sample Program

This script sets variables for base URL, user ID, and current date. Then it builds a URL dynamically using these variables. Finally, it checks that the URL includes the user ID.

Postman
// Set environment variables
pm.environment.set("baseUrl", "api.example.com");
pm.environment.set("userId", "12345");
pm.environment.set("currentDate", new Date().toISOString().split('T')[0]);

// Use dynamic URL in request
const url = `https://${pm.environment.get("baseUrl")}/users/${pm.environment.get("userId")}?date=${pm.environment.get("currentDate")}`;

console.log("Request URL:", url);

// Example assertion to check URL contains userId
pm.test("URL contains userId", function () {
    pm.expect(url).to.include(pm.environment.get("userId"));
});
OutputSuccess
Important Notes

Always define your variables before using them in URLs to avoid errors.

Use environment variables to switch easily between different servers or setups.

Check your URL after building it to make sure variables replaced correctly.

Summary

Dynamic URL building uses variables to make URLs flexible and reusable.

Postman uses {{variableName}} syntax to insert variables in URLs.

Setting and checking variables helps keep your tests reliable and easy to update.