# Use current value of Variable in Example Body Response

**URL:** <https://community.postman.com/t/use-current-value-of-variable-in-example-body-response/25090>\
**Category:** Help Hub\
**Tags:** variables\
**Created:** [June 22, 2021, 5:24pm UTC](https://community.postman.com/t/use-current-value-of-variable-in-example-body-response/25090 "2021-06-22T17:24:38Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomasmarques](https://avatars.discourse-cdn.com/v4/letter/t/ee7513/32.png) [@tomasmarques](https://community.postman.com/u/tomasmarques)\
**Post date:** [June 22, 2021, 5:24pm UTC](https://community.postman.com/t/use-current-value-of-variable-in-example-body-response/25090/1 "2021-06-22T17:24:39Z")

</div>

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:

```auto
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:

```auto
{
    "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?

---

<div class="post-metadata">

**Author:** ![jency.stella19](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/jency.stella19/32/16633_2.png) [@jency.stella19](https://community.postman.com/u/jency.stella19)\
**Post date:** [June 24, 2021, 9:01am UTC](https://community.postman.com/t/use-current-value-of-variable-in-example-body-response/25090/2 "2021-06-24T09:01:04Z")

</div>

Hi @tomasmarques

Welcome to the Community 🎉

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

 ![image](https://us1.discourse-cdn.com/getpostman/original/2X/0/0910720968b477ccd02123f9a4c13dba610375b7.png)

Also the variables are getting set correctly.

Are you still facing this issue?

---

<div class="post-metadata">

**Author:** ![tomasmarques](https://avatars.discourse-cdn.com/v4/letter/t/ee7513/32.png) [@tomasmarques](https://community.postman.com/u/tomasmarques)\
**Post date:** [June 24, 2021, 12:23pm UTC](https://community.postman.com/t/use-current-value-of-variable-in-example-body-response/25090/3 "2021-06-24T12:23:04Z")

</div>

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**
>
> ```auto
> {
> "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!

---

<div class="post-metadata">

**Author:** ![jency.stella19](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/jency.stella19/32/16633_2.png) [@jency.stella19](https://community.postman.com/u/jency.stella19)\
**Post date:** [June 24, 2021, 12:48pm UTC](https://community.postman.com/t/use-current-value-of-variable-in-example-body-response/25090/4 "2021-06-24T12:48:12Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![tomasmarques](https://avatars.discourse-cdn.com/v4/letter/t/ee7513/32.png) [@tomasmarques](https://community.postman.com/u/tomasmarques)\
**Post date:** [June 24, 2021, 1:25pm UTC](https://community.postman.com/t/use-current-value-of-variable-in-example-body-response/25090/5 "2021-06-24T13:25:15Z")

</div>

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

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

```

While the expected would be:

```auto
{
    "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

---

<div class="post-metadata">

**Author:** ![jency.stella19](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/jency.stella19/32/16633_2.png) [@jency.stella19](https://community.postman.com/u/jency.stella19)\
**Post date:** [June 25, 2021, 2:46am UTC](https://community.postman.com/t/use-current-value-of-variable-in-example-body-response/25090/6 "2021-06-25T02:46:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tomasmarques](https://avatars.discourse-cdn.com/v4/letter/t/ee7513/32.png) [@tomasmarques](https://community.postman.com/u/tomasmarques)\
**Post date:** [June 25, 2021, 9:50am UTC](https://community.postman.com/t/use-current-value-of-variable-in-example-body-response/25090/7 "2021-06-25T09:50:15Z")

</div>

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](https://us1.discourse-cdn.com/getpostman/original/2X/7/721d33108274f6413f7e8458672ce4e2ee1f5013.png)

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:

 ![Screenshot 2021-06-25 at 10.45.09](https://us1.discourse-cdn.com/getpostman/original/2X/2/2de90f1f125088e983f66a03dd54c21c8b8ead73.png)

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.

 ![Screenshot 2021-06-25 at 10.45.39](https://us1.discourse-cdn.com/getpostman/original/2X/3/317eb37a076cba30ed429c9eb10d349b08396799.png)

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.

 ![Screenshot 2021-06-25 at 10.51.22](https://us1.discourse-cdn.com/getpostman/original/2X/a/a4a40b920859797134e36d2699de11c1f4e99dbc.png)

I hope this makes the issue clearer

---

<div class="post-metadata">

**Author:** ![jency.stella19](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/jency.stella19/32/16633_2.png) [@jency.stella19](https://community.postman.com/u/jency.stella19)\
**Post date:** [June 28, 2021, 10:14am UTC](https://community.postman.com/t/use-current-value-of-variable-in-example-body-response/25090/8 "2021-06-28T10:14:14Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![henry.martinez](https://avatars.discourse-cdn.com/v4/letter/h/51bf81/32.png) [@henry.martinez](https://community.postman.com/u/henry.martinez)\
**Post date:** [June 23, 2022, 9:04pm UTC](https://community.postman.com/t/use-current-value-of-variable-in-example-body-response/25090/9 "2022-06-23T21:04:19Z")

</div>

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”.

---

<div class="post-metadata">

**Author:** ![matthijs-triple](https://avatars.discourse-cdn.com/v4/letter/m/7feea3/32.png) [@matthijs-triple](https://community.postman.com/u/matthijs-triple)\
**Post date:** [July 19, 2023, 1:10pm UTC](https://community.postman.com/t/use-current-value-of-variable-in-example-body-response/25090/10 "2023-07-19T13:10:32Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![KadinH](https://avatars.discourse-cdn.com/v4/letter/k/d26b3c/32.png) [@KadinH](https://community.postman.com/u/KadinH)\
**Post date:** [May 22, 2025, 1:52am UTC](https://community.postman.com/t/use-current-value-of-variable-in-example-body-response/25090/11 "2025-05-22T01:52:40Z")

</div>

I’ve got a similar use case to have a pre-request script set a variable for the mock server to use and return in the response and I’ve hit the same issue.

Has this been addressed at all?

The only workaround I have seen that _could_ work (I haven’t tried it) is to set the initial value from the pre-request script by making a call via the Postman API. But I understand that will consume your request allocation count per month, so it isn’t a viable option.
