bruno icon indicating copy to clipboard operation
bruno copied to clipboard

Random variable in json body

Open standej opened this issue 2 years ago • 2 comments

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?

standej avatar Oct 16 '23 16:10 standej

Hi, same question for header and postman {{$randomUUID}}

UnleashSpirit avatar Oct 18 '23 11:10 UnleashSpirit

@standej it should work if you use bru.setVar instead of bru.setEnvVar

KaniZ0r avatar Oct 19 '23 06:10 KaniZ0r

Hi @KaniZ0r I tried to do with collection variables and it still dont work: Here is my json body: image Here is what is created in database: image And here is value of my variable when i click to eye up: image 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

standej avatar Oct 19 '23 18:10 standej

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/

sebastien-savalle avatar May 20 '24 08:05 sebastien-savalle

I usually do something like this:

const { v4: uuidv4 } = require('uuid');
const inputName=uuidv4();
bru.setEnvVar("input_name", inputName);
image

Then in the json body: image

robertolopezlopez avatar Aug 01 '24 08:08 robertolopezlopez

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.

matus-m avatar Jan 16 '25 15:01 matus-m