Mock Server - Using Variables in Example Response

Short Story

You have to have your environment variables set in the init value columns if you want the mock server to see them.

Long story

So, this took me a while to figure out why I couldnā€™t make the whole mock envā€™s work ā€¦ and as it makes sense once I thought about ā€¦ I thought I would pass it along here so that others might not have the same issue I have :slight_smile:

So, first off, I tend to not use the environment variables like others seem to :stuck_out_tongue: I set all my environment variables that are ā€œdynamicā€ through a pre-script in the ā€œsetupā€ folder that I make for all my collections ā€¦ there is a corresponding ā€œtakedownā€ folder that removes all the stuff I did. For that reason, all most all my variables that are in ā€œcurrentā€ set.

Issue

So ā€¦ for the longest time I couldnā€™t get anything but the dynamic global variables like $randomBankAccount or $randomUUID to work.

The fix and reason for it

Then I read one of the posts about how they couldnā€™t figure out how to get the variables to not persist so that if a test were running twice then the variables would step on each other. That got me thinking along the right track and it ALL came clear ā€¦ the current variables CANā€™T be shared with the mock server cause they arenā€™t serialized or stored on the postman servers.

If only

Thus, I am trying to figure out how to ā€œforceā€ through code, the variables to the mock server so it can be used. Personally, I am still thinking that giving it ( the mock server instance ) access to headers is a simple and obvious thing to do is give something like: {{$header['the_header_you_want']}} as a form of a global expand like the $random is.
Thoughts?

6 Likes