# Get dynamic value in Pre-request script from Request Body

**URL:** <https://community.postman.com/t/get-dynamic-value-in-pre-request-script-from-request-body/8998>\
**Category:** Help Hub\
**Created:** [November 14, 2019, 3:55am UTC](https://community.postman.com/t/get-dynamic-value-in-pre-request-script-from-request-body/8998 "2019-11-14T03:55:27Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Paul2109](https://avatars.discourse-cdn.com/v4/letter/p/22d042/32.png) [@Paul2109](https://community.postman.com/u/Paul2109)\
**Post date:** [November 14, 2019, 3:55am UTC](https://community.postman.com/t/get-dynamic-value-in-pre-request-script-from-request-body/8998/1 "2019-11-14T03:55:27Z")

</div>

Hi all,

I’m trying to figure out how to get the dynamic variables from the Request Body. These are the 2 samples:

* * *

**1. SAMPLE** - using ‘pm.request.body’

**Pre-request Script:**

> var moment = require(‘moment’);  
> pm.variables.set(“genRefId”, moment().format(“YYYYMMDDHHmmssSSS”));
> 
> var content = pm.request.body; \<---- _i used pm.request.body_  
> console.log(content);

**Request Body:**

> {  
> “referenceId”:“{{genRefId}}”,  
> “billerCode”:“PBA”,  
> “billReferenceNo1”:“1234567”,  
> “billReferenceNo2”:“”,  
> “amount”:“3.00”  
> }

I also tried this but no luck.

* * *

**2. SAMPLE** - using JSON

**Pre-request Script:**

> var moment = require(‘moment’);  
> pm.variables.set(“genRefId”, moment().format(“YYYYMMDDHHmmssSSS”));
> 
> var obj = {  
> “referenceId”:“{{genRefId}}”,  
> “billerCode”:“PBAC”,  
> “billReferenceNo1”:“1234567”,  
> “billReferenceNo2”:“”,  
> “amount”:“3.00”  
> };  
> pm.globals.set(“rawBody”, JSON.stringify(obj));

**Request Body:**

> {{rawBody}}

* * *

**Expected:**

> {  
> “referenceId”:“20191114114923123”, \<— this is dynamic  
> “billerCode”:“PBAC”,  
> “billReferenceNo1”:“1234567”,  
> “billReferenceNo2”:“”,  
> “amount”:“3.00”  
> }

---

<div class="post-metadata">

**Author:** ![odanylewycz](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/odanylewycz/32/3240_2.png) [@odanylewycz](https://community.postman.com/u/odanylewycz)\
**Post date:** [November 14, 2019, 4:37am UTC](https://community.postman.com/t/get-dynamic-value-in-pre-request-script-from-request-body/8998/2 "2019-11-14T04:37:40Z")

</div>

Hi @Paul2109!

Welcome to the community! 👏

In regards to your question, I am not too sure what you are asking for.

In your last sample, you create a “genRefId” var, and set it as an environment variable. I would imagine then you have your dynamic variable that you are looking for? Then in your environment variable (genRefId), you would see that dynamic value, as pointed out in Sample 2. Every time it executed it would change. Now if you wanted to persist it, you could set it to a new variable everytime in your pm.environment.set().

If you can clarify a bit more exactly what you are expecting, I would be happy to help more.

Regards,  
Orest

---

<div class="post-metadata">

**Author:** ![Paul2109](https://avatars.discourse-cdn.com/v4/letter/p/22d042/32.png) [@Paul2109](https://community.postman.com/u/Paul2109)\
**Post date:** [November 14, 2019, 4:48am UTC](https://community.postman.com/t/get-dynamic-value-in-pre-request-script-from-request-body/8998/3 "2019-11-14T04:48:46Z")

</div>

Hi @odanylewycz

I am actually looking for a way to generate a unique number and use it in the Request Body. And then, get the whole request body with a generated unique number and set it in a global / environment variable.

1. Generate unique number in Pre-request Script
2. Use the unique number in Request Body

> {  
> “referenceId”:“{{genRefId}}”, \<---- e.g. 201911141246100  
> “billerCode”:“PBA”,  
> “billReferenceNo1”:“1234567”,  
> “billReferenceNo2”:“”,  
> “amount”:“3.00”  
> }

1. Get the Request Body with the generated unique number

> {  
> “referenceId”:“201911141246100”,  
> “billerCode”:“PBA”,  
> “billReferenceNo1”:“1234567”,  
> “billReferenceNo2”:“”,  
> “amount”:“3.00”  
> }

Basically, I just need to get Request Body as above and set it in the global / environment variable

---

<div class="post-metadata">

**Author:** ![odanylewycz](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/odanylewycz/32/3240_2.png) [@odanylewycz](https://community.postman.com/u/odanylewycz)\
**Post date:** [November 14, 2019, 5:15am UTC](https://community.postman.com/t/get-dynamic-value-in-pre-request-script-from-request-body/8998/4 "2019-11-14T05:15:09Z")

</div>

Hi @Paul2109,

Gotcha.

1. You can generate a unique number every time by using the following in the Pre-Request Script:  
`var ts = Math.round((new Date()).getTime() / 1000);`

That will get you the time in epoch time, which you can convert if necessary to a more readable format, but it should be unique every time.

1. Create your Body to the following in the Pre-Request Script:

2. Set your Body as a variable, as you had done before

This should provide you the functionality you are looking for.

Let me know if it works!

Regards,  
Orest

---

<div class="post-metadata">

**Author:** ![Paul2109](https://avatars.discourse-cdn.com/v4/letter/p/22d042/32.png) [@Paul2109](https://community.postman.com/u/Paul2109)\
**Post date:** [November 14, 2019, 6:22am UTC](https://community.postman.com/t/get-dynamic-value-in-pre-request-script-from-request-body/8998/5 "2019-11-14T06:22:07Z")

</div>

It works! Thank you so much

---

<div class="post-metadata">

**Author:** ![fifsky](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/fifsky/32/4952_2.png) [@fifsky](https://community.postman.com/u/fifsky)\
**Post date:** [July 14, 2020, 10:28am UTC](https://community.postman.com/t/get-dynamic-value-in-pre-request-script-from-request-body/8998/6 "2020-07-14T10:28:20Z")

</div>

I know what you mean, I want to do the same thing. I need to sign the body, but the body use variables. Eventually I found a solution in the documentation:

[https://learning.postman.com/docs/writing-scripts/script-references/variables-list/](https://learning.postman.com/docs/writing-scripts/script-references/variables-list/)

`body = pm.variables.replaceIn(pm.request.body.raw);`

Then:

`CryptoJS.MD5(body+pm.collectionVariables.get("app_secret")).toString();`

I hope it’s helpful to those of you in the back

---

<div class="post-metadata">

**Author:** ![onishi.sayuri](https://avatars.discourse-cdn.com/v4/letter/o/9d8465/32.png) [@onishi.sayuri](https://community.postman.com/u/onishi.sayuri)\
**Post date:** [October 1, 2020, 10:22am UTC](https://community.postman.com/t/get-dynamic-value-in-pre-request-script-from-request-body/8998/7 "2020-10-01T10:22:43Z")

</div>

this is very helpful, thank you for this solution 🙂

---

<div class="post-metadata">

**Author:** ![teymurov](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/teymurov/32/5845_2.png) [@teymurov](https://community.postman.com/u/teymurov)\
**Post date:** [October 7, 2020, 8:09am UTC](https://community.postman.com/t/get-dynamic-value-in-pre-request-script-from-request-body/8998/8 "2020-10-07T08:09:11Z")

</div>

Hey Buddy, I logged in to say thank you ✌

---

<div class="post-metadata">

**Author:** ![guilherme.pacheco](https://avatars.discourse-cdn.com/v4/letter/g/3ec8ea/32.png) [@guilherme.pacheco](https://community.postman.com/u/guilherme.pacheco)\
**Post date:** [December 28, 2020, 10:19pm UTC](https://community.postman.com/t/get-dynamic-value-in-pre-request-script-from-request-body/8998/9 "2020-12-28T22:19:59Z")

</div>

Thanks mannnnn!!! Works!

---

<div class="post-metadata">

**Author:** ![William\_M](https://avatars.discourse-cdn.com/v4/letter/w/c89c15/32.png) [@William\_M](https://community.postman.com/u/William_M)\
**Post date:** [March 17, 2021, 6:29pm UTC](https://community.postman.com/t/get-dynamic-value-in-pre-request-script-from-request-body/8998/10 "2021-03-17T18:29:34Z")

</div>

Thank you @fifsky, it works

---

<div class="post-metadata">

**Author:** ![flight-technologist7](https://avatars.discourse-cdn.com/v4/letter/f/9dc877/32.png) [@flight-technologist7](https://community.postman.com/u/flight-technologist7)\
**Post date:** [November 15, 2022, 7:29am UTC](https://community.postman.com/t/get-dynamic-value-in-pre-request-script-from-request-body/8998/11 "2022-11-15T07:29:12Z")

</div>

Hi  
I have this pre-request script

const moment = require(‘moment’);  
postman.setEnvironmentVariable(“requestid”, moment().format(“0223YYYYMMDDHHmmss000000”));

I want to be the “requestid” to be unique every time.  
first request: “0223YYYYMMDDHHmmss000001”  
second request: “0223YYYYMMDDHHmmss000002”  
.  
.  
.

and so on until let’s say 1000 requests.  
Basically, I need to make the last 6 digits dynamic.

Regards

---

<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:** [August 23, 2023, 2:50pm UTC](https://community.postman.com/t/get-dynamic-value-in-pre-request-script-from-request-body/8998/12 "2023-08-23T14:50:42Z")

</div>

Closed on August 23, 2023, 2:50pm UTC

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