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 aconfig.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 and Create a mock.
Working with an AI coding agent? The Postman plugin can build and run mocks for you.
For current help, see the Postman docs, try the free Postman tools (no account needed), or join us on Discord.
I have a mocked collection of requests whose example responses are dependent on the previous response’s returned data. Requests are always made sequentially in this order:
-
/channels (Populates initial channel data)
Sets the allChannels environment variable in the test scripts which is simply an array of objects. -
/channels/:id
Sets a selectedChannelId from thepm.request.url.path[1]and a selectedChannel from looking up the channel by id in the allChannels list in the pre-request script. -
/: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"} -
/users/:id
Sets a selectedUser from thepm.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.
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
Channel by id response / environment & Mock example response
- Shows {{selectedChannel}} not populating
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.unsetafter 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
*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.




