I’m trying to reuse my URL parameters inside my mock examples.
For example:
/mock?param=ABC
would ideally reply
{
"param" = "ABC"
}
/mock?param=BCD
would reply
{
"param" = "BCD"
}
etc
In my example I use the env var “depots” and I set it in 2 different places (because I don’t which place is best, to be honest), but it always replies "{{depots}}" as you can see below.
Following up here as mentioned. Now that you’ve created the environment variable and associated it to the Mock Server, in your particular case if you want to programmatically update the Environment Variable to then reflect the appropriate value in the response you can make use of a pre-request script. More information can be found here:
Try the following and let me know if it helps. In your pre-request script, write the following:
let param = pm.request.url.query.toObject().depots;
pm.environment.set("depots",param);
What this does is that it takes the value of the Query Param, depots and then assigns that value to the Environment Variable depots.
So when you make the changes here:
Note that the Example response still references the Environment Variable:
The response will change. Please note that you’ll need to make sure to persists the current value to initial value in your settings:
Otherwise, the initial value won’t change and your response value won’t either. There may be a slight delay as the changes need to be synced and Mock Servers are essentially designed to server Static Content. I hope this helps. Feel free to follow up here if you still need help.
As a final note, the pre-request script will work when making the request from the Native App. If you’re opening the Browser and making the request that way, unfortunately, the pre-request script will not run so the value of depots will only reflect the last value in Initial Value. If that value isn’t changed (i.e. through re-request script ) then the response value won’t be the latest one. I hope this clarifies.
As a final note, the pre-request script will work when making the request from the Native App . If you’re opening the Browser and making the request that way, unfortunately, the pre-request script will not run so the value of depots will only reflect the last value in Initial Value . If that value isn’t changed (i.e. through re-request script ) then the response value won’t be the latest one.
Sadly this is exactly my use case : we make requests from outside of Postman on Postman mock servers
So I guess I must conclude that what I’m trying to do is not possible.