postmanlabs / postman-app-support

Postman is an API platform for building and using APIs. Postman simplifies each step of the API lifecycle and streamlines collaboration so you can create better APIs—faster.
https://www.postman.com
5.84k stars 839 forks source link

Collection Variables not returning in Mock Server Example Response Body #8502

Open bmccreath opened 4 years ago

bmccreath commented 4 years ago

Describe the bug Collection Variables are not returned in the mock server example response body, while environment variables are. There should be no difference in behaviour between these variables.

To Reproduce Steps to reproduce the behavior:

  1. Create a mock server
  2. In a sample request pre-request script, update the value of a collection variable
  3. In an example response, use this variable in the body
  4. The response body should fill the "{{sampleCollectionVariable}}" field with the value of the variable. It does not - but it works for environment variables.

Expected behavior The response body should show the updated value of the collection variable. There should be no difference in behaviour between collection variables, environment variables and global variables in the response body.

Screenshots Pre Request Script Setting CollectionVariable, Environment Variable, and Global Variable Pre Request Script Setting CollectionVariable, Environment Variable, and Global Variable

200 Success Example, showing that all 3 variable types are used 200 Success Example, showing that all 3 variable types are used

Console log, showing that all 3 variables have successfully updated Console log, showing that all 3 variables have successfully updated

200 Success Response, showing that only the environment variable has been correctly parsed 200 Success Response, showing that only the environment variable has been correctly parsed

App information (please complete the following information):

Additional context Working with Postman Mock Servers, trying to use collection variables as only mocking a single collection without different environments.

rheasunshine commented 3 years ago

Apparently, pre-request scripts are not executed for mock server requests. I wish! Does anyone know if this is a feature in the works?

colbsmcdolbs commented 3 years ago

@rheasunshine Do you know if Test scripts are run using Mock Server requests? I'm starting to think know, I can get them to trigger when hitting my server endpoint locally, but not when one of my Monitors hits it. Thanks!

mccreathbecky commented 3 years ago

@colbyjayallen I'm fairly sure test scripts / pre-request scripts are only run locally, or using a framework like Newman. e.g. if you were sending a request to a mock server from an external system to postman (e.g. from a mobile app, anything not in postman itself) than the scripts don't run.

rproman commented 2 years ago

@vvs11, @dushyantchillale - Hi. Will Collection variables be supported soon in Mock Server response body? Is it really intended that we should be using Environment variables in Mock Server response body?

Dhembla commented 2 years ago

@bmccreath @rproman Collections variables are now supported on mock servers. You can specify collection variables in your examples, and the mock server will resolve them while returning the response. Thanks for your patience 🙇.

DanShoubridge commented 2 years ago

... you were sending a request to a mock server from an external system to postman (e.g. from a mobile app, anything not in postman itself) than the scripts don't run.

The ability for pre-request scripts to run before the response is returned would be very handy, for example being able to set a variable to today's date in a specific date format (e.g. YYYYMMDD) would be useful

cburns2017 commented 1 year ago

Following up here because I've seen this question asked multiple times in different places. What is the best practice for using a variable like the "today's date" example in the question above? Is this not possible with Postman?

alessiochiffi commented 1 year ago

Hi I need to set a collection variable through the pre-request scripts. Does anyone know any workaround for this to get it working with mock servers? thanks