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

**URL:** https://community.gatling.io/t/create-a-random-uuid-and-assign-to-a-variable-for-using-in-a-header/7450
**Category:** Gatling (Open-Source)
**Created:** [December 21, 2022, 3:51pm UTC](https://community.gatling.io/t/create-a-random-uuid-and-assign-to-a-variable-for-using-in-a-header/7450 "2022-12-21T15:51:15Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![vineeth.maddela](https://avatars.discourse-cdn.com/v4/letter/v/898d66/32.png) [@vineeth.maddela](https://community.gatling.io/u/vineeth.maddela)
#### Post date: [December 21, 2022, 3:51pm UTC](https://community.gatling.io/t/create-a-random-uuid-and-assign-to-a-variable-for-using-in-a-header/7450/1 "2022-12-21T15:51:16Z")

</div>

Hello!  
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.

```java
//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}"));

```

---

<div class="post-metadata">

### Author: ![slandelle](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/slandelle/32/4_2.png) [@slandelle](https://community.gatling.io/u/slandelle)
#### Post date: [December 21, 2022, 6:35pm UTC](https://community.gatling.io/t/create-a-random-uuid-and-assign-to-a-variable-for-using-in-a-header/7450/2 "2022-12-21T18:35:34Z")

</div>

Have you properly checked the documentation?

- [Gatling - What's New in 3.9](https://gatling.io/docs/gatling/reference/current/whats_new/3.9/#core)
- [Gatling - Expression Language](https://gatling.io/docs/gatling/reference/current/core/session/el/#built-in-functions)

---

<div class="post-metadata">

### Author: ![GeMi](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/gemi/32/939_2.png) [@GeMi](https://community.gatling.io/u/GeMi)
#### Post date: [December 21, 2022, 8:33pm UTC](https://community.gatling.io/t/create-a-random-uuid-and-assign-to-a-variable-for-using-in-a-header/7450/3 "2022-12-21T20:33:46Z")

</div>

Additionally you have here:

> [@Regex Validator - how to do it right](https://community.gatling.io/t/regex-validator-how-to-do-it-right/7436):
>
> I wondering how to do correct validate if returned uuid is in correct format. I have created custom validator using Regex matches function. What do you think about that: package pl.gemiusz; import io.gatling.javaapi.core.ScenarioBuilder; import io.gatling.javaapi.core.Simulation; import io.gatling.javaapi.http.HttpProtocolBuilder; import java.util.regex.Pattern; import static io.gatling.javaapi.core.CoreDsl.\*; import static io.gatling.javaapi.http.HttpDsl.http; public class Case0020RegexV…

A sample how to use `#{randomUuid()}` in Java 🙂

---

<div class="post-metadata">

### Author: ![vineeth.maddela](https://avatars.discourse-cdn.com/v4/letter/v/898d66/32.png) [@vineeth.maddela](https://community.gatling.io/u/vineeth.maddela)
#### Post date: [December 23, 2022, 10:14am UTC](https://community.gatling.io/t/create-a-random-uuid-and-assign-to-a-variable-for-using-in-a-header/7450/4 "2022-12-23T10:14:37Z")

</div>

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.

```java
  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));**
    System.out.println(newSession); 
    return newSession;
  })

```

---

<div class="post-metadata">

### Author: ![slandelle](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/slandelle/32/4_2.png) [@slandelle](https://community.gatling.io/u/slandelle)
#### Post date: [December 23, 2022, 10:23am UTC](https://community.gatling.io/t/create-a-random-uuid-and-assign-to-a-variable-for-using-in-a-header/7450/5 "2022-12-23T10:23:10Z")

</div>

Please read the documentation: [Gatling - Session API](https://gatling.io/docs/gatling/reference/current/core/session/session_api/#setting-attributes)

---

<div class="post-metadata">

### Author: ![vineeth.maddela](https://avatars.discourse-cdn.com/v4/letter/v/898d66/32.png) [@vineeth.maddela](https://community.gatling.io/u/vineeth.maddela)
#### Post date: [December 23, 2022, 10:42am UTC](https://community.gatling.io/t/create-a-random-uuid-and-assign-to-a-variable-for-using-in-a-header/7450/6 "2022-12-23T10:42:48Z")

</div>

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

---

<div class="post-metadata">

### Author: ![sbrevet](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/sbrevet/32/830_2.png) [@sbrevet](https://community.gatling.io/u/sbrevet)
#### Post date: [December 23, 2022, 11:19am UTC](https://community.gatling.io/t/create-a-random-uuid-and-assign-to-a-variable-for-using-in-a-header/7450/7 "2022-12-23T11:19:48Z")

</div>

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

For your example, I suggest to chain the `set`:

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

```

Cheers!

---

<div class="post-metadata">

### Author: ![vineeth.maddela](https://avatars.discourse-cdn.com/v4/letter/v/898d66/32.png) [@vineeth.maddela](https://community.gatling.io/u/vineeth.maddela)
#### Post date: [December 27, 2022, 11:01am UTC](https://community.gatling.io/t/create-a-random-uuid-and-assign-to-a-variable-for-using-in-a-header/7450/8 "2022-12-27T11:01:43Z")

</div>

That worked. Thank you.

---

<div class="post-metadata">

### Author: ![sbrevet](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/sbrevet/32/830_2.png) [@sbrevet](https://community.gatling.io/u/sbrevet)
#### Post date: [December 27, 2022, 5:44pm UTC](https://community.gatling.io/t/create-a-random-uuid-and-assign-to-a-variable-for-using-in-a-header/7450/9 "2022-12-27T17:44:24Z")

</div>


