How to pass dynamic json request body in postman

I have a POST request where I need to pass some parameters dynamically, as my code checks for a duplicate entry. I tried to write a preliminary script request and then set global variables and tried to access it in my request. This does not work. PFB details

Script Advance Request

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

Body

{"request": {"Name": "{{FirstName}}", "MiddleName": "mani", "LastName": "{{}} last name"}}

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

+6
source share
2 answers

You can do this by adding

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

in the Script pre-query section.

And then adding

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

in body.

I tried this in both Postman and Newman, and works great on creating a random first and last name.

+10
source
 { "request": { "firstName":"{{$randomInt}}", "middleName":"mani", "lastName":"{{$randomInt}}" } } 

No need to add global variables. Postman has a dynamic variable {{$randomInt}} that adds a random integer from 0 to 1000

+4
source

Source: https://habr.com/ru/post/1245067/


All Articles