Random variable in json body
I exported my tests from postman and imported them in Bruno. In lots of tests I used postman built in feature for random username {{$randomUserName}} or random number {{$randomInt}} and I was putting this dynamic values in json body of the call.
https://learning.postman.com/docs/writing-scripts/script-references/variables-list/
Postman example if i set companyNumber env variable in pre request:
{ "companyNumber": "{{companyNumber}}" }
The problem is that Bruno didnt imported it properly and i can't find a way to put in json body dynamic random environmental variable (string or int).
I tried something like this in script pre request so I get random number:
bru.setEnvVar("companyNumber", Math.round(Math.random() * 100000000));
I checked and Bruno randomize proper value and set env variable properly but i cant put this value inside request json body from env variable.
And in body I tried something like this:
{ "companyNumber": "{{companyNumber}}" }
but without success.
So my question is how I can put dynamic random variable in json body of the request in Bruno?
Hi, same question for header and postman {{$randomUUID}}
@standej it should work if you use bru.setVar instead of bru.setEnvVar
Hi @KaniZ0r I tried to do with collection variables and it still dont work:
Here is my json body:
Here is what is created in database:
And here is value of my variable when i click to eye up:
And here is my pre request script how i generate random value:
bru.setVar("loginVar", Math.round(Math.random() * 100000));
Bruno version 0.25.0
Unfortunately still not working and do not interpolate value
It would be nice to provide a feature that allows to generate random values in the json payload directly to simplify the usage. It could be something similar to the templating feature in Mockoon.
https://mockoon.com/docs/latest/templating/overview/
I usually do something like this:
const { v4: uuidv4 } = require('uuid');
const inputName=uuidv4();
bru.setEnvVar("input_name", inputName);
Then in the json body:
If you have a collection of hundreds of requests, where lots of requests needs some dynamic value - e.g. idempotency header, correlation keys etc, then it is not practical having to add the same script to each request. Postman solution with {{$randomUUID}} and other dynamic variables is really useful for a lot of people and I see no negative impact on how Bruno works.