Create a Random UUID and assign to a variable for using in a header

I want to create a variable and assign a Random UUID(Unique identifier) to it so that I can pass this as a header for my API request. I am not able to do this in Gatling.
In Jmeter, I am able to do it very simply by using ${__UUID()} and assigning to a User Parameter.

Note: I am doing a PoC to evaluate if Gatling fits to our organization.

I tried using the below but it does not work: Any ideas on how to solve it? Documentation says I need to use Session API but I am not sure how to use. Please help.

//importing the necessary Java Utility.
import java.util.UUID;

//generate a random UUID in the Simulation Class.
UUID ninid = UUID.randomUUID();
String ninIDString = ninid.toString();

//Pass the above created variable in my header.
Private Map<CharSequence, String> headers_0 = Map.ofEntries(
Map.entry("X-Correlation-Id", "#{ninIDString}"));

Have you properly checked the documentation?

1 Like

Additionally you have here:

A sample how to use #{randomUuid()} in Java :slight_smile:

Hello again!
I was able to generate a UUID at random and assign to the session variable for using in the header later but I am having trouble in using the session.setAll function, so that I can set multiple random UUID variables in the session for using them later. The below is what I am doing but I am unable to get it to work.

Any help in the setAll syntax would be great.

  private ScenarioBuilder scn = scenario("SeedPartySimulation")
  .exec(session -> {
    String uuid = UUID.randomUUID().toString();
    String ninid = UUID.randomUUID().toString();
    Session newSession = **session.setAll(("userUUID", uuid),("nin_id", ninid));**
    return newSession;

Please read the documentation: Gatling - Session API

Hi, I read but not enough info there, hence asking.

As your IDE should scream at you, setAll take a collection (an Iterable)

For your example, I suggest to chain the set:

Session newSession = session.set("userUUID", uuid).set("nin_id", ninid);


That worked. Thank you.