-1

I am trying to generate a random number using {{$randomInt}} and writing this script in pre-request script to get and set the value in an environment

let rand_int = pm.variables.replaceIn('{{$randomInt}}')
pm.environment.set("rand_int", rand_int)

But I want to use {{$randomInt}} directly in the body and store the value directly in the environment from the request body. Is there any way to do it?

Suppose this is my request body. When I send the request I want to auto-set that random value in the environment.

{
   "gårdsnummer": {{$randomInt}}
}
VLAZ
  • 26,331
  • 9
  • 49
  • 67

1 Answers1

0

When you're using an environment variable in the body of the request you don't need or shouldn't use the $. The following should work:

{
    "gårdsnummer": {{rand_int}}
} 
so cal cheesehead
  • 2,515
  • 4
  • 29
  • 50
  • What I'm assuming here is that you're trying to use the random int you generated and then proceeded to save with environment variable `rand_int` – so cal cheesehead Jun 03 '22 at 19:55
  • Thanks for your answer. But this is not what I want. If there is any way so that no need to write the pre-request script. In the request body, I want to use {{$randomInt}} and then hit send button that data is automatically saved to the environment. – Nazmul Hossain Jun 06 '22 at 07:33
  • That's not something you can do in the body of the request, you can generate a random int in the body but not save it to the environment, that's a function of the pre-request script or you could do it in the test tab after you receive the response. – so cal cheesehead Jun 15 '22 at 21:59