Mock Server - Using Variables in Example Response

examples
response
mockservers

#1

Hello

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?

Jamie


#2

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.


#3

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:

Jamie


#4

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


#5

@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.


#6

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

Regards
Jamie


#7

@pratik

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?


#8


#9

@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}}"
}