using the below code in the Pre-request works, but it does not work in the Post-request.
let pathVariables = pm.request.url.variables;
pathVariables.each(function(variable){
console.log("testing values: " + variable.key + ": " + variable.value);
});
I want to use the variables passed to the API to test against the results without having to create new variables every time (Final goal is to have this code placed in a Collections Folder so every request can be tested with it’s unique key and value at the folder level instead of each individual request.)
Hey @mike-stolpa-secura-n 
Welcome to the Postman Community! 
Could you provide an example of the URL structure that you have been using this against?
pm.request.url.variables relate to path variables.
The following is an example request against Postman Echo using a single path variable.
I have the following in both the pre-request script and post-response script.
console.log(pm.request.url);
The resulting console logs.
I suspect what this is doing is that during execution, the pm.request function fills in the relevant information and therefore the post-response script can only return the completed values.
If you want the initial values, you will need to do it within the pre-request script. (Which you can still set at the folder level).
It does appear the variable is no longer available after the run…
Any suggestions on how to get around this? Maybe set a variable in the pre, use it in the post and then do an .unset on the variable to make sure the next test in the collection doesn’t have it available unless it creates it again.
Yeah I think your idea is the current correct workaround for this. You wouldn’t even have to unset anything if you used a request-level variable. It’s only scoped to that one request
pm.variables.set('myPathVariable', 'hey!');
What is it you are trying to achieve here?
What are you trying to test?
Path Variables in Postman is a Postman Feature.
It doesn’t really have anything specifically to do with the API, apart from make it easier to reuse the same request for multiple end points by allowing you to change the path without editing the URL.
You need to have that value for the path stored somewhere first!
You mention a unique key and value for each request. Where is that coming from?
The value for the Path Variable it its self can be a variable which you just need to enclose in curly brackets {{variableName}}. You can also just set a variable in the URL instead of a path variable (as this doesn’t really look like a path element).
I was able to solve the missing information by putting this in the Pre-request:
pm.variables.set("request_url", pm.request.url);
I can now get the original URL in the Post-request as I was hoping. I’m using this to dynamically test the key data returned in my response body against what was asked for.
Simple fix by putting this in the pre-request at the folder level of my collection.
1 Like