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 :
To get all path variable in a list:
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:-
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.