Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass dynamic json request body in postman

I have a POST request where i need to pass some parameters dynamically since my code is checking for a duplicate entry. I tried writing a pre request script and then setting the global variables and tried to access it in my request. It is not working. PFB, the details

Pre-request Script

postman.setGlobalVariable("firstname", (text + parseInt(Math.random()*10000)).toString()); postman.setGlobalVariable("lastname", text + parseInt(Math.random()*10000));

Body

{ "request": { "firstName":"{{firstname}}", "middleName":"mani", "lastName":"{{lastname}}" } }

Here firstName is getting passed as {{firstname}} instead of random string.

like image 482
Div Avatar asked Mar 15 '16 08:03

Div


1 Answers

You ca do it by adding

var rnd = Math.floor((Math.random() * 10000) + 1);
postman.setEnvironmentVariable("firstname", "fname"+rnd);
postman.setEnvironmentVariable("lastname", "lname"+rnd);

in the Pre-request Script section.

And then adding

{ 
"firstName":"{{firstname}}", 
"middleName":"mani", 
"lastName":"{{lastname}}" 
} 

in the body.

I tried it in both Postman and Newman and is working perfectly generating a random first name and last name.

like image 129
AeJey Avatar answered Sep 26 '22 07:09

AeJey