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

Leave a Reply

Your email address will not be published. Required fields are marked *