# Mock response body not populating from pre-request set environment variables

**URL:** <https://community.postman.com/t/mock-response-body-not-populating-from-pre-request-set-environment-variables/7757>\
**Category:** Help Hub\
**Tags:** mocks, variables, pre-request\
**Created:** [September 12, 2019, 9:42pm UTC](https://community.postman.com/t/mock-response-body-not-populating-from-pre-request-set-environment-variables/7757 "2019-09-12T21:42:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kbrinso](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/kbrinso/32/2767_2.png) [@kbrinso](https://community.postman.com/u/kbrinso)\
**Post date:** [September 12, 2019, 9:42pm UTC](https://community.postman.com/t/mock-response-body-not-populating-from-pre-request-set-environment-variables/7757/1 "2019-09-12T21:42:41Z")

</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).

I have a [mocked collection](https://www.getpostman.com/collections/a4764f8b5c205057d251) of requests whose example responses are dependent on the previous response’s returned data. Requests are always made sequentially in this order:

1. /channels (Populates initial channel data)  
Sets the **allChannels** environment variable in the test scripts which is simply an array of objects.

2. /channels/:id  
Sets a **selectedChannelId** from the `pm.request.url.path[1]` and a **selectedChannel** from looking up the channel by id in the **allChannels** list in the pre-request script.

3. /:channel\_id/threads ( Creates a random creator user id and username for each thread )  
Sets a **currentUsers** object environment variable in test scripts where user ids are keys and usernames are the properties.  
`{"creator_id": 901,"creator_username": "Rodrick.Mayer34"}`

4. /users/:id  
Sets a **selectedUser** from the `pm.request.url.path[1]` and a **selectedUsername** from looking up the channel by id in the **currentUsers** list in the pre-request script.

\*_Note: This implementation exclusively using environment variables._

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/0/2/028606ddb81d3f962e0156757ba89f926ee7d50e.png)  
Above, we can see that all environment variables are in-fact being properly set in each of the corresponding pre-request scripts.

\*_Note: All response/request headers have been set to `content-type: application/json` as mentioned in previous threads as a potential issue_

# The Problem - bug?

Both /users/:id and /channels/:id are not properly populating the example reponse body from the **{{selectedUsername}}** and **{{selectedChannel}}** environment variables set in pre-request scripts.

#### User by id response / environment & Mock example response

- Shows **{{selectedUsername}}** not populating  
 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/1/1/11956278c60477c80322913610e1559c90554187.png)  
 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/2/3/2362849e1eaf888408ecedbd78285245ace97f9a.png)

#### Channel by id response / environment & Mock example response

- Shows **{{selectedChannel}}** not populating  
 ![image](https://us1.discourse-cdn.com/getpostman/original/2X/c/cda5c045a03c2e6cc239a42970d75f395364d2ea.png)  
 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/7/7/778333588d325eb13d654d941dfd93d339f8bc68.png)

# What I’ve tried

- Every configuration I can think of for content-type
- Using globals / using variables
- Every possible syntax variation I could think of for dynamically populating the example response body
- Logging the environment variables in the pre-request scripts works as expected
- Tried `pm.environment.unset` after the requests have been made in the test scripts to see if resetting things makes a difference
- Read all of the available docs and blog posts
- Scoured this forum and stackoverflow

# Note

Thank you for any help and suggestions… I’m really at a loss on this one. Here is the collection again: [https://www.getpostman.com/collections/a4764f8b5c205057d251](https://www.getpostman.com/collections/a4764f8b5c205057d251)

\*_Remember: Requests are intended to be made in the above order. In order to replicate this issue you’d have to copy correct ids etc from previous responses for the next response._

---

<div class="post-metadata">

**Author:** ![singhsivcan](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/singhsivcan/32/1595_2.png) [@singhsivcan](https://community.postman.com/u/singhsivcan)\
**Post date:** [September 13, 2019, 5:12am UTC](https://community.postman.com/t/mock-response-body-not-populating-from-pre-request-set-environment-variables/7757/2 "2019-09-13T05:12:15Z")

</div>

Hello @kbrinso, welcome to the community! 👋

As I can see from the attached collection, you’re updating the environment using the pre-request script and expecting the mock to use that updated environment value.

Are you using the collection runner/monitor to make these requests?

---

<div class="post-metadata">

**Author:** ![kbrinso](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/kbrinso/32/2767_2.png) [@kbrinso](https://community.postman.com/u/kbrinso)\
**Post date:** [September 13, 2019, 5:51am UTC](https://community.postman.com/t/mock-response-body-not-populating-from-pre-request-set-environment-variables/7757/3 "2019-09-13T05:51:02Z")

</div>

Hi @singhsivcan, thanks for the quick response and welcome to the community!

I’m not using the collection runner to sequentially run the requests. I’m doing so manually to verify they were at least presenting the expected behavior before writing tests. Do you think it’s worth setting up tests and automating running each of the requests in the collection runner? It does seem that all my data is being properly set in the environment. At the same time, I don’t quite understand why/how it’s possible for the collection runner to return something different than manually running the requests sequentially. Anyways, I’ll try just about anything at this point. I’ll test this out now and share if I find anything different from running these in the collection runner.

---

<div class="post-metadata">

**Author:** ![singhsivcan](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/singhsivcan/32/1595_2.png) [@singhsivcan](https://community.postman.com/u/singhsivcan)\
**Post date:** [September 13, 2019, 6:07am UTC](https://community.postman.com/t/mock-response-body-not-populating-from-pre-request-set-environment-variables/7757/4 "2019-09-13T06:07:06Z")

</div>

Oh, I was just asking because actually the collection runner runs the collection in a sandboxed environment, and only after the collection runner has finished the run will it update the environment on sync.  
So, that’s why I was asking, because that would break the case.

However, since you’re running it manually this should’ve ideally worked.

Now,  
On going through your pre-request script, I am unable to understand what exactly you’re doing.

1. In your 1st request, `channels/:id`,

```auto
// Why this? You've just set the selectedChannelId - with that value
const chanData = pm.environment.get(pm.request.url.path[1].toString());

// Did you mean
const chanId = pm.environment.get("selectedChannelId");
pm.environment.set("selectedChannelData", chanId);

```

What exactly is going on here?  
We just read the value from the environment that is of the selectedChannelId and then we’ve set the same value as the `selectedChannelData`.

I am guessing, you need to dig into your scripts, there seems to be some confusion here.

Also,  
If you’re updating the environment from a pre-request script and then expecting the mock to use that updated value while the request is ran, it wouldn’t happen.

Why?  
Because as soon as you send a request, the request is ran in a sandboxed environment, and the variables that are updated are actually updated after the whole request execution has finished and after the tests scripts have also finished running, is when the change in the environment is actually synced to the server and that’s when the mock will be using the updated environment.

If you want to update the environment on the fly through the pre-request script then you need to use the [Postman API](https://api.getpostman.com) in the pre-request script along with the help of [`pm.sendRequest`](https://learning.getpostman.com/docs/postman/scripts/postman_sandbox_api_reference/#pmsendrequest)

I was unable to debug it further given the lack of data on my end.

---

<div class="post-metadata">

**Author:** ![kbrinso](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/kbrinso/32/2767_2.png) [@kbrinso](https://community.postman.com/u/kbrinso)\
**Post date:** [September 13, 2019, 5:04pm UTC](https://community.postman.com/t/mock-response-body-not-populating-from-pre-request-set-environment-variables/7757/5 "2019-09-13T17:04:24Z")

</div>

Ah, thank you so much! This is just the clarification I was looking for. I was really hoping that this flow could work within the Postman app, but it seems that I’d have to use the API in order for this to work.
