Skip to content

How To Use Path Variable In Postman

API Testing Blog

Understanding Path Variables in Postman

Path variables are dynamic segments in a URL that can be replaced with values during runtime. This allows you to create a single API request that can be utilized for multiple endpoints, making your testing more efficient.

How to Define Path Variables in Postman

  1. Locate the URL Field: Open your Postman request and go to the URL field.
  2. Insert Brackets: Place your path variable within curly braces ({}) in your URL. For example, if you’re testing a user endpoint, you might use /users/{userId}.

Using Path Variables in Postman Requests

  1. Create a New Environment: In the Postman environment, create a new variable and name it userId. Set its value to the specific user ID you want to test with.
  2. Reference the Environment Variable: In the URL field of your request, replace the placeholder ({userId}) with the environment variable using double curly braces ({{userId}}).

Example Request:

GET {{baseUrl}}/users/{{userId}}

Example Environment:

  • userId: 1234

Explanation:

  • {{baseUrl}}: References a base URL variable in your Postman environment for your API.
  • {{userId}}: References the userId variable you defined in your environment. This will dynamically replace the {userId} in your URL.

Sending Requests With Path Variables

Once you’ve defined your path variable and environment, you can send your request in Postman. Postman will automatically replace the {{userId}} placeholder with the value from your environment.

How to Use Multiple Path Variables in Postman

You can use multiple path variables in a single request.

Example with two path variables:

GET {{baseUrl}}/users/{{userId}}/posts/{{postId}}

Example Environment:

  • userId: 1234
  • postId: 5678

Explanation:

  • {{baseUrl}}: References the base URL variable from your Postman environment.
  • {{userId}}: References the userId variable defined in your environment.
  • {{postId}}: References the postId variable defined in your environment.

How to Set Path Variable Values Dynamically: Using Pre-request Scripts

For more complex scenarios, you might want to dynamically set path variable values before sending your request.

Example Pre-request Script:

pm.environment.set("userId", 1234); // Set the userId variable value
pm.variables.set("postId", 5678); // Set the postId variable value

Explanation:

  • This script uses the Postman Runtime API to set the values of the userId and postId variables before your request is executed. This allows you to dynamically fetch values from other sources or manipulate data before sending your request.

Passing Path Variables in the Request Body

While path variables are commonly used in URLs, you can also include them in the request body. This is useful for scenarios where your API requires a specific structure in the request data.

Example Request Body:

{
"userId": "{{userId}}"
}

Explanation:

  • The userId value is retrieved from your Postman environment and automatically filled in before the request is sent.

Conclusion

Path variables in Postman are a powerful tool for making your API testing more efficient and flexible. By leveraging path variables and environment variables, you can create reusable requests that can adapt to various data points. By incorporating pre-request scripts, you can achieve even greater dynamism and control over your path variable values.

API Testing Blog