Mock Server - Using Variables in Example Response


I have set up a mock server, with a very simple example JSON response:

"client_ref": "zzzzzzzz"

This is working fine. However I’ve also set up some variables, and I’m unable to create an example response that references the variables. I have tried with local environment variables, and also with global variables.

So for instance I have a variable called ‘postMockINC’ that I wish to use in place of “zzzzzzzz” above.

I have tried updating the JSON in my example response body to this:

    "client_ref": "{{postMockINC}}"

and also to this:

    "client_ref": {{postMockINC}}

but in both cases the literal values are returned. Is it possible to reference variables in example JSON responses?


Hey @JamieW1972, it is indeed possible to reference variables in example JSON responses. While creating the mock however, you should select the environment in which you have the variables, you would then be able to reference the environment variables.

Can you try once and let me know if it works? Thanks.

Hello Deepak,

Thank you for your response. I created a new collection, environment, variables, request and mock response. Now I am getting a blank where the the variable value should be. This is at least a step forward from just getting back the literal, but still not as I had expected. I’ve attached a couple of screenshots that hopefully show how I have set this up, and also show the results. I hope this helps you to help me :wink:


And here is the second screenshot (new users can only post one attachment at a time)…variables_in_mock_response

@JamieW1972 - Can you set content-type to application/json in the Headers of your Example Response? That could be one reason why the variable resolution is not working as expected.

Ahhh-ha. Thank you Pratik… yes, that has solved it! :slight_smile:



I have followed the same steps as Jamie above yet my variable is still not being parsed. The headers for the response are not pictured but do include “Content-Type”: “application/json”. Can you take a look?

@mblasius - The content-type header has been set correctly. Can you try double quoting the environment variable in your example like this? Also while creating the Mock Server you need to specify the environment that has this variable along with the collection that you are mocking.

     "challenge" : "{{challenge}}"
1 Like

I setup similar @mblasius, but not work

I see not yet support varriable on example response

What helped me was setting the environment in the “Browse” tab.

Browse > Mocks > (Find your mock) > Edit > Change the environment to the desired one.

For some reason it looks like setting the environment in the Build mode does not actually apply for the mock

1 Like

Hi @kevin.chiu, Thanks for writing in!
Could you tell us more about how you’re using mock server and how you were setting environment from the Build mode?

Setting the environment via the dropdown in the top right of the Build mode did not seem to actually change the environment, so I changed the environment of the Mock server in the “Browse” tab

(Also I have Content-Type set to application/json).

Hope that helps

Got it!
You have to choose the environment that you want to associate with the mock server while creating one. You can do that when you create Mock server from Collection Browser view ( The window that opens when you click :arrow_forward: next to collection). This associated environment will now store all the variables associated with the mock server.


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.


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.