# Get session variable in scala func

**URL:** <https://community.gatling.io/t/get-session-variable-in-scala-func/6322>\
**Category:** Gatling (Open-Source)\
**Created:** [November 22, 2021, 2:13pm UTC](https://community.gatling.io/t/get-session-variable-in-scala-func/6322 "2021-11-22T14:13:45Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mike](https://avatars.discourse-cdn.com/v4/letter/m/58f4c7/32.png) [@Mike](https://community.gatling.io/u/Mike)\
**Post date:** [November 22, 2021, 2:13pm UTC](https://community.gatling.io/t/get-session-variable-in-scala-func/6322/1 "2021-11-22T14:13:45Z")

</div>

Hey community! I have a bunch of requests like:

```java

val headers1 = Map(
"SomeHeader1" -> "${SomeHeader1}",
"SomeHeader2" -> "${SomeHeader2}",
"SomeHeader3" -> "${SomeHeader3}")

val headers2 = Map(
"SomeHeader4" -> "${SomeHeader4}",
"SomeHeader5" -> "${SomeHeader5}",
"SomeHeader6" -> "${SomeHeader6}")

val req1 = http("req")
.get("/some/path")
.headers(headers1)

val req2 = http("req")
.get("/some/path")
.headers(headers2)

```

req1 and req2 are the same except headers, so I want to have a common request which will sent either headers1 or headers2 in .headers section depending on some session variable, which I will define on scenario level.

```java
val commonReq = http("req")
.get("/some/path")
.headers(hFunction())

```

Is it possible to get a session variable into a function like def hFunction(): Map[String, String] = {}, which will set appropriate header depending on some logic?

Any help would be appreciated.

---

<div class="post-metadata">

**Author:** ![Mike](https://avatars.discourse-cdn.com/v4/letter/m/58f4c7/32.png) [@Mike](https://community.gatling.io/u/Mike)\
**Post date:** [November 22, 2021, 6:01pm UTC](https://community.gatling.io/t/get-session-variable-in-scala-func/6322/2 "2021-11-22T18:01:07Z")

</div>

Also, I tried to use session api, but can’t understand how to use code like session =\> session(“header”).asString not in exec section

понедельник, 22 ноября 2021 г. в 17:15:31 UTC+3, Niko Kovatch:

---

<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:** [November 22, 2021, 9:33pm UTC](https://community.gatling.io/t/get-session-variable-in-scala-func/6322/3 "2021-11-22T21:33:27Z")

</div>

headers (plural) is one of the rare places where you can’t pass a function yet.

---

<div class="post-metadata">

**Author:** ![Mike](https://avatars.discourse-cdn.com/v4/letter/m/58f4c7/32.png) [@Mike](https://community.gatling.io/u/Mike)\
**Post date:** [November 23, 2021, 6:59am UTC](https://community.gatling.io/t/get-session-variable-in-scala-func/6322/4 "2021-11-23T06:59:58Z")

</div>

Thank you for your answer Stephane. Is there any elegant workaround, what do you think?

вторник, 23 ноября 2021 г. в 00:33:58 UTC+3, Stéphane Landelle:

---

<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:** [November 23, 2021, 7:50am UTC](https://community.gatling.io/t/get-session-variable-in-scala-func/6322/5 "2021-11-23T07:50:52Z")

</div>

There’s no way to use a function to pass the headers’ Map, sorry.

What you can do though is share the common request pieces:

val headers1 = Map(  
“SomeHeader1” → “${SomeHeader1}”,  
“SomeHeader2” → “${SomeHeader2}”,  
“SomeHeader3” → “${SomeHeader3}”)

val headers2 = Map(  
“SomeHeader4” → “${SomeHeader4}”,  
“SomeHeader5” → “${SomeHeader5}”,  
“SomeHeader6” → “${SomeHeader6}”)

val base = http(“req”)  
.get("/some/path")

val req1 = base  
.headers(headers1)

val req2 = base  
.headers(headers2)

---

<div class="post-metadata">

**Author:** ![Mike](https://avatars.discourse-cdn.com/v4/letter/m/58f4c7/32.png) [@Mike](https://community.gatling.io/u/Mike)\
**Post date:** [November 23, 2021, 8:42am UTC](https://community.gatling.io/t/get-session-variable-in-scala-func/6322/6 "2021-11-23T08:42:32Z")

</div>

It sounds reasonable, thank you!

вторник, 23 ноября 2021 г. в 10:51:24 UTC+3, Stéphane Landelle:
