Postman Tutorial Part 33 – Retrieve Path Variables Value in Tests Script in Postman

We have already seen Usage of Path variables in Postman. We may need to retrieve provided path variables for assertion in scripts in Postman. We have already seen about Retrieving query parameters in tests in Postman. In this post we will see retrieving Path variables in tests in Postman.

We can retrive provided path variables only in “Pre-request script” as once request is hit , path variables can not be retrieved as final URI is constructed using path variables. You will get null values if try to retrieve path variables in “Tests” tab. If you want to use path variables values in “Tests” tab, you can store in environment or global variable and use it.

To get value of specific path variable :
pm.request.url.variables.get(‘id’);

To get all path variable in a list:
pm.request.url.variables.all();

Let’s create a URI with Path variables :-

Add below code to “Pre-request Scripts” to retrieve path variables:-

Hit the request and check Postman console:-

Import above example collection from here:-

https://www.getpostman.com/collections/c7b0ba6698adfd1f897f

Refer basics of API Testing here.

Refer detailed tutorial of Postman here.

More about API Testing in upcoming posts. Stay tuned.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappyApiTesting

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 6+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went through so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.
%d bloggers like this: