Get dynamic value in Pre-request script from Request Body

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

Hi @Paul2109!

Welcome to the community! :clap:

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

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

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:

    var obj = {
    “referenceId”: ts,
    “billerCode”:“PBAC”,
    “billReferenceNo1”:“1234567”,
    “billReferenceNo2”:"",
    “amount”: “3.00”
    };

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

    pm.globals.set(“rawBody”, JSON.stringify(obj));

This should provide you the functionality you are looking for.

Let me know if it works!

Regards,
Orest

It works! Thank you so much

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/

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

4 Likes

this is very helpful, thank you for this solution :slight_smile:

Hey Buddy, I logged in to say thank you :v:

Thanks mannnnn!!! Works!

Thank you @fifsky, it works

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

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