Use current value of Variable in Example Body Response

Hi there,

I’m building a mock API in Postman with requests and example responses for each.
I’d like to use variables in the example body response but they are not working as expected.

I have the following code in the Pre-request Script:

pm.globals.set("v1", "1");
console.log(pm.globals.get("v1"))
pm.collectionVariables.set("v2", "2");
console.log(pm.collectionVariables.get("v2"))
pm.environment.set("v3", "3");
console.log(pm.environment.get("v3"))
pm.variables.set("v4", "4");
console.log(pm.variables.get("v4"))

And then the example body is:

{
    "1": "{{v1}}",
    "2": "{{v2}}",
    "3": "{{v3}}",
    "4": "{{v4}}"
}

Only v3 is actually being replaced with a value, however the value being printed is the “Initial Value” of the environmental variable instead of the “Current Value” (that is being updated by the pre-request script).

(This is a shared workspace so there is no auto-persist of variables)

How can I get the current value of the environmental variable in the response body of an example instead of the initla value?

Hi @tomasmarques

Welcome to the Community :tada:

I tried to execute the script you shared it is workign for me as expected

Also the variables are getting set correctly.

Are you still facing this issue?

No, I still have the same issue. Now, I was trying to replicate it again and it’s even worse as none of the variables are being replaced.

Here is exported json from the collection I’m using

postman_collection.json
{
	"info": {
		"_postman_id": "9ee633e8-e3ee-446a-b2c8-b1300ee4583e",
		"name": "Testing Variables",
		"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
	},
	"item": [
		{
			"name": "Request",
			"event": [
				{
					"listen": "prerequest",
					"script": {
						"exec": [
							"pm.globals.set(\"v1\", \"1\");",
							"console.log(pm.globals.get(\"v1\"))",
							"pm.collectionVariables.set(\"v2\", \"2\");",
							"console.log(pm.collectionVariables.get(\"v2\"))",
							"pm.environment.set(\"v3\", \"3\");",
							"console.log(pm.environment.get(\"v3\"))",
							"pm.variables.set(\"v4\", \"4\");",
							"console.log(pm.variables.get(\"v4\"))"
						],
						"type": "text/javascript"
					}
				}
			],
			"request": {
				"method": "GET",
				"header": [],
				"url": {
					"raw": "{{mock-url}}/test-endpoint",
					"host": [
						"{{mock-url}}"
					],
					"path": [
						"test-endpoint"
					]
				}
			},
			"response": [
				{
					"name": "Response",
					"originalRequest": {
						"method": "GET",
						"header": [],
						"url": {
							"raw": "{{mock-url}}/test-endpoint",
							"host": [
								"{{mock-url}}"
							],
							"path": [
								"test-endpoint"
							]
						}
					},
					"status": "OK",
					"code": 200,
					"_postman_previewlanguage": "json",
					"header": [
						{
							"key": "Content-Type",
							"value": "application/json",
							"description": "",
							"type": "text"
						}
					],
					"cookie": [],
					"body": "{\n    \"1\": \"{{v1}}\",\n    \"2\": \"{{v2}}\",\n    \"3\": \"{{v3}}\",\n    \"4\": \"{{v4}}\"\n}"
				}
			]
		}
	],
	"event": [
		{
			"listen": "prerequest",
			"script": {
				"type": "text/javascript",
				"exec": [
					""
				]
			}
		},
		{
			"listen": "test",
			"script": {
				"type": "text/javascript",
				"exec": [
					""
				]
			}
		}
	],
	"variable": [
		{
			"key": "v2",
			"value": "oldvalue2"
		}
	]
}

Besides this I have a mock server and an environment (with the mock-url variable).
Also, not sure if relevant but I’m using a GET request, not a POST.

Can you share the configurations you are using to get it working?
Thanks!

I’m not using any specific configuration here just used the script you shared in postman echo its working.

Can you share the response that you are currently receiving also what is expected in a more detailed way?

The response I’m getting is the same as the example body

{
    "1": "{{v1}}",
    "2": "{{v2}}",
    "3": "{{v3}}",
    "4": "{{v4}}"
}

While the expected would be:

{
    "1": "1",
    "2": "2",
    "3": "3",
    "4": "4"
}

I was asking if you could share the example definition, also if you could try with a postman mock server instead of postman-echo maybe that’s where it does not work

Sure @tomasmarques I’ll check it from mock server and will update you.

I see what you were doing now.

I’ve created a collection and placed the code that sets the variables in the Pre-request script of the collection. Then I created a Request inside and added an Example response to the request:

Screenshot 2021-06-25 at 10.44.40

If I use postman-echo and set the Body of the Request to use those variables the echoed response is correctly replaced with the new values:

However, that is not what I need. I’m building a mock API, so a webpage would make a request to the mock server and the response that is sent is the one with the variables.
For that I’m using an Example inside the Request where I set the body with variables.
And the problem is when the client makes the request, the variables are not being replaced.

If I use the mock server, the only variable that is replaced is the environment variable, and it shows the “initial value” instead of the “current value” that is set in the pre-request script.

I hope this makes the issue clearer

1 Like

Hi @tomasmarques yes I was using the postman echo like how you did hence was able to get the variable correctly. Haven’t tried with Mock this is new to me, will try to check on it if can help you with it.

Was there a solution?.. I too, have the same issue where the “Current Value” is not being applied in the body of a response, only the “Initial Value”. I’m setting environment variables from the pre-script test and it only sets the “Current Value”.

I can confirm this is an issue, experiencing the same exact problem.

The current value is never picked up by an example response body, only the initial value. It’s an issue if you have any dynamic values you want to inject in your response body…

Any comments if this can be solved?