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