Use environment variable in mock examples

Hi there

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.

Thank you!
Alexandre

Hey @alexandre.perrachon,

Welcome to the community! :rocket:

When you first created the Mock server, did to select that as the Environment file?

The mock server will use the variable values from the selected environment in the request and response.

Here’s an example of what I think you trying to do:

Hi Danny

Thank you for this quick reply
After seing your message I changed my mock server to set it on my env.


And it works : my env variable called “depots” is indeed used in my example set with {{depots}}.

But I’m really trying to do is to return in the response the parameter passed in the request :


would ideally be :

Maybe the env variable is not the answer for this ?

Thank you
Alexandre

Hi @alexandre.perrachon,

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:

https://learning.postman.com/docs/postman/scripts/pre-request-scripts/#scripting-before-your-request-runs

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:

image

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.

Hi SabriH

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 :weary:

So I guess I must conclude that what I’m trying to do is not possible.

Calling, for example from my webbrowser: postmanmockserver.com/apiendpoint?param=TOTO
and getting as a response (dynamically):
{
“param” : “toto”
}

The closest I think you could get to this right now would be if the variable was part of the path and not the param

I posted a demo of this here:

From the browser that would resolve the value in the response body but that’s still not what you’re after. :frowning: