# Alternating responses to the same mock server endpoint

**URL:** <https://community.postman.com/t/alternating-responses-to-the-same-mock-server-endpoint/40046>\
**Category:** Help Hub\
**Tags:** mockservers\
**Created:** [October 13, 2022, 6:08pm UTC](https://community.postman.com/t/alternating-responses-to-the-same-mock-server-endpoint/40046 "2022-10-13T18:08:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![benigoat](https://avatars.discourse-cdn.com/v4/letter/b/f07891/32.png) [@benigoat](https://community.postman.com/u/benigoat)\
**Post date:** [October 13, 2022, 6:08pm UTC](https://community.postman.com/t/alternating-responses-to-the-same-mock-server-endpoint/40046/1 "2022-10-13T18:08:59Z")

</div>

> **Archived topic — some details may be out of date.**  
> Mock servers built from saved collection examples are still supported. Postman now also has **code-based mocks** : JavaScript request handlers plus a `config.yaml`, stored as files in your Git repository, which you can run locally or deploy. Create one in the app, or use the Postman CLI to generate one from an OpenAPI spec, collection or dataset (`postman mock generate`) and run it (`postman mock run`). See [About mocks](https://learning.postman.com/docs/design-apis/mock-apis/mocks/overview) and [Create a mock](https://learning.postman.com/docs/design-apis/mock-apis/mocks/create-a-mock).  
> Working with an AI coding agent? The [Postman plugin](https://github.com/postmanlabs/postman-plugin) can build and run mocks for you.  
> For current help, see the [Postman docs](https://learning.postman.com/docs/), try the free [Postman tools](https://www.postman.com/tools) (no account needed), or join us on [Discord](https://discord.gg/58aNNsRBGR).

**Your question may already have an answer on the community forum. Please search for related topics, and then read through the [guidelines](https://community.postman.com/t/about-the-just-getting-started-category/37) before creating a new topic.**

> Here’s an outline with best practices for making your inquiry.

_My question_:  
Hi, long story short is I’m looking for a way to alternate the return value of a mock server endpoint from one request to the other. My mock server is based on an API my app service is expected to hit; it will repeatedly hit a specific endpoint for data until the return ID eventually returns as a blank string rather than a Guid (see below), providing an exit condition for my loop.

![image](https://us1.discourse-cdn.com/getpostman/original/3X/a/d/ad447423cddd40a26f492212cecf87bff9f7e31d.png)

I have created an example for the endpoint where a ‘returnUuid’ collection variable is returned, and written a pre-requisite script that will update the value of that variable based on its current value, alternating between a new random guid and an empty string.

Hitting the endpoint repeatedly in Postman shows that this does work; however hitting it from either a browser or from my app shows the value not changing from whatever it happens to currently be set to. I may be making the incorrect assumption that scripts would run in this situation. If so then is there another way to acheive what I’m looking for here?

---

<div class="post-metadata">

**Author:** ![w4dd325](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/w4dd325/32/12436_2.png) [@w4dd325](https://community.postman.com/u/w4dd325)\
**Post date:** [October 13, 2022, 6:30pm UTC](https://community.postman.com/t/alternating-responses-to-the-same-mock-server-endpoint/40046/2 "2022-10-13T18:30:58Z")

</div>

Hi @benigoat

Assuming my understanding of your description is correct…  
“The pre-request script won’t run if you hit the API from a browser or app”.  
The pre-request is executed inside Postman before the API call.

---

<div class="post-metadata">

**Author:** ![benigoat](https://avatars.discourse-cdn.com/v4/letter/b/f07891/32.png) [@benigoat](https://community.postman.com/u/benigoat)\
**Post date:** [October 14, 2022, 8:25am UTC](https://community.postman.com/t/alternating-responses-to-the-same-mock-server-endpoint/40046/3 "2022-10-14T08:25:03Z")

</div>

Hi @w4dd325

I just need some clarification on when you say ‘inside Postman’; So if I set up a simple mock server endpoint with a PR script logging something to the console, and hit that URL through a browser, I don’t see anything in the console. Is this correct?

---

<div class="post-metadata">

**Author:** ![w4dd325](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/w4dd325/32/12436_2.png) [@w4dd325](https://community.postman.com/u/w4dd325)\
**Post date:** [October 15, 2022, 9:14am UTC](https://community.postman.com/t/alternating-responses-to-the-same-mock-server-endpoint/40046/4 "2022-10-15T09:14:35Z")

</div>

Correct, as far as my understanding goes, a browser/app submitting the API call will not trigger the pre-req or test scripts. These live inside Postman and would be triggered when the “Send” button is clicked.
