How to get Dynamic values using Pre-Request Script and concatenate

Hello Dears,
Hope everyone doing well.
I have the requirement to get values dynamically.
Base URL: http://localhost:8080/identityiq/rest/identities
Example :
Body :

{
 "firstname": "Anand",
"lastname": "Thivari" 
}

But here above I need to pass one more required variable is “name”. And “name” should be like “firstname”.“lastname”
Ex . Anand.Thivari
But I want the “name” variable as dynamically assign when the request is sent to the server.
Likewise, “email” also posts dynamically.
Ex. Anand@demo.com
Can anyone suggest how to resolve it?
Thanks in advance.!

  • Rakesh

Thank god, I resolved myself.

Method: POST
Base URL: http://localhost:8080/identityiq/rest/identities/
Body : json-raw

{
    "firstname" : "{{firstname}}",
    "lastname" : "{{lastname}}",
    "name" : "{{name}}",
    "email" : "{{email}}"
}

Pre-request Script:

var fname = "Aravind";
var lname = "Thivari";
var fullname = fname+"."+lname;
var email = fname+"@mail.com";

pm.environment.set("firstname",fname);
pm.environment.set("lastname",lname);
pm.environment.set("name",fullname);
pm.environment.set("email",email);

Thanks and pls suggest to me if have any other way.
-Rakesh

Hi @rakesh,

Instead of using the environment variable, you can also use, local variables if it is just a temporary thing.

For example,
pm.variables .set(“email”,email); // These are not direct visible like the environment variables, but of for a tempory thing it doessnt matter.

Or
pm.collectionVariables .set(“email”,email); // This is visible in your collection tab

Hope this helps.

Cheers.