Return dynamic data in Mock Server Response

I have a mock API server. I’m trying to return dynamic data when a request is made (i.e. different data per request, generated by code).
Just like how the dynamic variables work, unfortunately those are preconfigured.

So I have written a Pre-request script to update an environment variable.
And then I return the value of this variable in the response body.

However, because the variable doesn’t persist between Current and Initial Value,
the response body always returns the Initial value which makes the behaviour static.

Is there a solution to this? I’m open to other ways of going about it, thanks.

Hello @shakisparki :wave:

Welcome to the community.

You’re right, the initial value will be the preferred one in such cases, so you’d need to preserve the initial value of the variable.

In order to do so, you might have to use Postman’s API - specifically the PUT Update Environment endpoint

There was a similar discussion here: Passing environment variable between monitor runs, it’s regarding monitors, but the idea is the same (preserving the initial value of a variable)

1 Like

another thing you can do is to add multiple static example and return specific example by specifying custom headers like :

x-mock-response-name
x-mock-response-id

this will return specific example back , so you can add multiple example and dynamically call a specific example when required

Other way will be to use Postman API to persist the environment variable ,