# How can I put variables into a POST request Response body?

**URL:** <https://community.postman.com/t/how-can-i-put-variables-into-a-post-request-response-body/57605>\
**Category:** Help Hub\
**Tags:** variables, json\
**Created:** [February 13, 2024, 12:29pm UTC](https://community.postman.com/t/how-can-i-put-variables-into-a-post-request-response-body/57605 "2024-02-13T12:29:16Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![manoloe](https://avatars.discourse-cdn.com/v4/letter/m/eb9ed0/32.png) [@manoloe](https://community.postman.com/u/manoloe)\
**Post date:** [February 13, 2024, 12:29pm UTC](https://community.postman.com/t/how-can-i-put-variables-into-a-post-request-response-body/57605/1 "2024-02-13T12:29:16Z")

</div>

Greetings, I’ve created a mock server and am trying to make some more dynamic responses. I’ve got a post request that I want to essentially respond with a random number. Ive got the variable set up in the environment and in the pre request script have it generating a random number which is fine, but when I go to the response body and put into it: “{{randomNumber}}” and after sending the request I get returned an empty string. I can see in the environment quick look that a random number is definitely being generated but for some reason will not put that variable into the post request’s response body.

---

<div class="post-metadata">

**Author:** ![jetison](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/jetison/32/12_2.png) [@jetison](https://community.postman.com/u/jetison)\
**Post date:** [February 13, 2024, 6:46pm UTC](https://community.postman.com/t/how-can-i-put-variables-into-a-post-request-response-body/57605/2 "2024-02-13T18:46:01Z")

</div>

Mock servers rely on saved examples of requests and responses. I don’t believe you can use a saved example with a pre-request script to generate a random number that is returned in the mock response, in that way.

Instead, you could use a Faker variable, like `{{$randomInt}}` and save it in the example’s response body, like this screenshot here.

 ![Screenshot 2024-02-13 at 10.40.59 AM](https://us1.discourse-cdn.com/getpostman/original/3X/1/a/1ad60b021a953bf94bafd0732afed67766c544e9.png)

And a couple more ideas for generating “dynamic” behavior: [Fake it till you make it: mocks for agile development | by Joyce Lin | Better Practices | Medium](https://medium.com/better-practices/https-medium-com-postman-engineering-fake-it-till-you-make-it-mocks-for-agile-development-f4d050cad694)

---

<div class="post-metadata">

**Author:** ![manoloe](https://avatars.discourse-cdn.com/v4/letter/m/eb9ed0/32.png) [@manoloe](https://community.postman.com/u/manoloe)\
**Post date:** [February 14, 2024, 1:07pm UTC](https://community.postman.com/t/how-can-i-put-variables-into-a-post-request-response-body/57605/3 "2024-02-14T13:07:52Z")

</div>

Thanks for the reply. I’ve just been trying to get a random number between 1-10 rather than the {{$randomInt}} which is 0-1000. I kind of got it working by turning on the persistant variables but it changed to the first random number generated and stayed that way.

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [February 14, 2024, 1:43pm UTC](https://community.postman.com/t/how-can-i-put-variables-into-a-post-request-response-body/57605/4 "2024-02-14T13:43:51Z")

</div>

You should be able to use a pre-request script on the actual request to generate your random number. Which you can save to an collection variable that can be used in place of $randomInt.

Postman has the Lodash library available in the Sandbox and you can use the Lodash random function.

```auto
pm.collectionVariables.set("randomInt", _.random(10));

```

---

<div class="post-metadata">

**Author:** ![manoloe](https://avatars.discourse-cdn.com/v4/letter/m/eb9ed0/32.png) [@manoloe](https://community.postman.com/u/manoloe)\
**Post date:** [February 14, 2024, 2:49pm UTC](https://community.postman.com/t/how-can-i-put-variables-into-a-post-request-response-body/57605/5 "2024-02-14T14:49:05Z")

</div>

![image](https://us1.discourse-cdn.com/getpostman/original/3X/6/d/6d65224fa20c9cadec86707265a845d0a17a3df3.png)  
Here is the response body:  
 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/a/c/ac2029afc2b011ed93af34aaee3e815e385eaeb5.png)

And then here is what i get in the response:

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/4/3/439b9112e5fde86d760dcaf5b73b7d63c90e3f2d.png)

---

<div class="post-metadata">

**Author:** ![manoloe](https://avatars.discourse-cdn.com/v4/letter/m/eb9ed0/32.png) [@manoloe](https://community.postman.com/u/manoloe)\
**Post date:** [February 14, 2024, 2:49pm UTC](https://community.postman.com/t/how-can-i-put-variables-into-a-post-request-response-body/57605/6 "2024-02-14T14:49:44Z")

</div>

In the env quick look i can see it is a number and its being stored:

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/4/8/484fd591d8acc44336a11f997b31fb84ee46685d.png)

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [February 14, 2024, 3:54pm UTC](https://community.postman.com/t/how-can-i-put-variables-into-a-post-request-response-body/57605/7 "2024-02-14T15:54:04Z")

</div>

I haven’t tried this myself, so there is an assumption that you can do this the same as you can with the faker variables.

I’d have to set this up myself to prove it.

CollectionVariables are generally stored as strings so try removing the quotes for that element in the response and see if that makes any difference in the first instance.

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [February 14, 2024, 5:13pm UTC](https://community.postman.com/t/how-can-i-put-variables-into-a-post-request-response-body/57605/8 "2024-02-14T17:13:36Z")

</div>

Ok, so I’ve tested this and it doesn’t work.

The reason its not working is because the mock doesn’t seem to have access to the current values for collection or environment variables.

Which is probably correct as the mock runs from the Postman Cloud.

If you set a value in the initial value for a collection variable. It works, but this isn’t of much use in this scenario.

You can update the initial value using the Postman Collection API, but that seems overkill for what you are trying to achieve.

---

<div class="post-metadata">

**Author:** ![manoloe](https://avatars.discourse-cdn.com/v4/letter/m/eb9ed0/32.png) [@manoloe](https://community.postman.com/u/manoloe)\
**Post date:** [February 15, 2024, 9:14am UTC](https://community.postman.com/t/how-can-i-put-variables-into-a-post-request-response-body/57605/9 "2024-02-15T09:14:00Z")

</div>

just so i can check it out, how can I update the initial value using the collections?

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [February 15, 2024, 10:04am UTC](https://community.postman.com/t/how-can-i-put-variables-into-a-post-request-response-body/57605/10 "2024-02-15T10:04:38Z")

</div>

[Set initial value of collection variables - 🙋 Help - Postman Community](https://community.postman.com/t/set-initial-value-of-collection-variables/56274/12)

![image](https://us1.discourse-cdn.com/getpostman/original/3X/c/6/c6adab282975f585a7c67010560e46db37590c52.png)

You need to generate and include an API key in the headers (X-API-Key).

The above is a full request, but you can use sendRequest() in a pre-request script to the same effect.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/system/32/29796_2.png) [@system](https://community.postman.com/u/system)\
**Post date:** [March 16, 2024, 10:05am UTC](https://community.postman.com/t/how-can-i-put-variables-into-a-post-request-response-body/57605/11 "2024-03-16T10:05:29Z")

</div>

Closed on March 16, 2024, 10:05am UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
