# Hot to use session value in another function gatling

**URL:** <https://community.gatling.io/t/hot-to-use-session-value-in-another-function-gatling/3873>\
**Category:** Gatling (Open-Source)\
**Created:** [August 7, 2017, 8:13pm UTC](https://community.gatling.io/t/hot-to-use-session-value-in-another-function-gatling/3873 "2017-08-07T20:13:09Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![leela\_krishna](https://avatars.discourse-cdn.com/v4/letter/l/35a633/32.png) [@leela\_krishna](https://community.gatling.io/u/leela_krishna)\
**Post date:** [August 7, 2017, 8:13pm UTC](https://community.gatling.io/t/hot-to-use-session-value-in-another-function-gatling/3873/1 "2017-08-07T20:13:09Z")

</div>

I’m trying to save the session value and use that in other function. Here is what I’m trying

.exec ( session =\> session.set(“abc”, session(“value”).as[String]))

.exec (test.authTest)

object test{  
def authTest: String = {  
val x = “${abc}”  
return x  
}  
}

when I execute my gatling script, I’m seeing something like this

16:05:17.388 [ERROR] i.g.c.a.b.SessionHookBuilder$$anon$1 - ‘hook-1’ failed to execute: Can’t cast value of type class java.lang.String into class io.gatling.core.session.Session

How can I save a session value and use that in my next method or request. Please help if anyone know about this.  
Thanks in advance

---

<div class="post-metadata">

**Author:** ![Robert\_Bacon](https://avatars.discourse-cdn.com/v4/letter/r/278dde/32.png) [@Robert\_Bacon](https://community.gatling.io/u/Robert_Bacon)\
**Post date:** [August 7, 2017, 8:45pm UTC](https://community.gatling.io/t/hot-to-use-session-value-in-another-function-gatling/3873/2 "2017-08-07T20:45:15Z")

</div>

Hi Leela,

I ran into this earlier on with my own test. The key is to utilize a check to ensure you have the variable you are looking for, then to save it with saveAs. For example:

.check("$…value")  
.find  
.saveAs(“abc”)

Then, when you want to use “abc” again later, you should be able to just call it using “${abc}”.

I’ll be honest, I had some problems with this, but my issue was I was unintentionally using both the “”" triple quotes “”" and the ‘s’ for interpolation. Hopefully this will help you out!!!

Rob

---

<div class="post-metadata">

**Author:** ![leela\_krishna](https://avatars.discourse-cdn.com/v4/letter/l/35a633/32.png) [@leela\_krishna](https://community.gatling.io/u/leela_krishna)\
**Post date:** [August 8, 2017, 2:19pm UTC](https://community.gatling.io/t/hot-to-use-session-value-in-another-function-gatling/3873/3 "2017-08-08T14:19:48Z")

</div>

Hi Rob,  
I’m using a check in my HTTP request and then saved it as String like this  
val scn = scenario(“Test”)  
.feed(csvFeeeder)  
.exec(http(“Header generation”)  
.post("/auth")  
.headers(headers\_0)  
.formParam(“xxxx”, “zzzz”)  
.check(headerRegex(“value”,“abc\*”).saveAs(“value”))  
.check(status.is(401)) )

after saving that, I’m trying to use session to store the header value and use that in a different function like this  
.exec ( session =\> session.set(“abc”, session(“value”).as[String]))

.exec (test.authTest)

object test{  
def authTest: String = {  
val x = “${abc}”  
return x  
}  
}

But when I tried to get the session value, I’m seeing that error. I’m not sure what’s wrong with the approach.

---

<div class="post-metadata">

**Author:** ![Robert\_Bacon](https://avatars.discourse-cdn.com/v4/letter/r/278dde/32.png) [@Robert\_Bacon](https://community.gatling.io/u/Robert_Bacon)\
**Post date:** [August 8, 2017, 4:03pm UTC](https://community.gatling.io/t/hot-to-use-session-value-in-another-function-gatling/3873/4 "2017-08-08T16:03:18Z")

</div>

I went through a lot of that too. I went through the whole session.set and session(“value”).as[String] and none of them worked for me either. What finally worked for me was to simplify it.

Try this:

.check(headerRegex(“value”,“abc”).saveAs(“value”))

.exec(test.authTest)

object test{  
def authTest:String = {  
val x = “${value}”  
return x  
}  
}

Though another issue I see that might cause you some problems is the defining of authTest after you call it. Is there a way you can put the def authTest portion in front of the .exec(test.authTest) ?

---

<div class="post-metadata">

**Author:** ![leela\_krishna](https://avatars.discourse-cdn.com/v4/letter/l/35a633/32.png) [@leela\_krishna](https://community.gatling.io/u/leela_krishna)\
**Post date:** [August 8, 2017, 7:07pm UTC](https://community.gatling.io/t/hot-to-use-session-value-in-another-function-gatling/3873/5 "2017-08-08T19:07:41Z")

</div>

I tried like this, but encountered with this error ‘;’ expected but ‘.’ found  
object test{  
def authTest: String = {  
val x = “${value}”  
println(x)  
return x  
}  
}

.exec (test.authTest)

---

<div class="post-metadata">

**Author:** ![Robert\_Bacon](https://avatars.discourse-cdn.com/v4/letter/r/278dde/32.png) [@Robert\_Bacon](https://community.gatling.io/u/Robert_Bacon)\
**Post date:** [August 8, 2017, 7:52pm UTC](https://community.gatling.io/t/hot-to-use-session-value-in-another-function-gatling/3873/6 "2017-08-08T19:52:31Z")

</div>

Try taking out the ‘.’ before exec. It is expecting exec to start a new chain builder, but the ‘.’ says its adding onto a chain.

---

<div class="post-metadata">

**Author:** ![leela\_krishna](https://avatars.discourse-cdn.com/v4/letter/l/35a633/32.png) [@leela\_krishna](https://community.gatling.io/u/leela_krishna)\
**Post date:** [August 8, 2017, 8:02pm UTC](https://community.gatling.io/t/hot-to-use-session-value-in-another-function-gatling/3873/7 "2017-08-08T20:02:24Z")

</div>

Thanks, Rob. The error was gone but, I’m not seeing the value of the “${value}” parameter. Its just printing ${value}

---

<div class="post-metadata">

**Author:** ![Robert\_Bacon](https://avatars.discourse-cdn.com/v4/letter/r/278dde/32.png) [@Robert\_Bacon](https://community.gatling.io/u/Robert_Bacon)\
**Post date:** [August 8, 2017, 8:11pm UTC](https://community.gatling.io/t/hot-to-use-session-value-in-another-function-gatling/3873/8 "2017-08-08T20:11:47Z")

</div>

Oh, I think you don’t need the quotes. Try setting val x = ${value}. I think that should work. Here is hoping.

---

<div class="post-metadata">

**Author:** ![leela\_krishna](https://avatars.discourse-cdn.com/v4/letter/l/35a633/32.png) [@leela\_krishna](https://community.gatling.io/u/leela_krishna)\
**Post date:** [August 8, 2017, 8:16pm UTC](https://community.gatling.io/t/hot-to-use-session-value-in-another-function-gatling/3873/9 "2017-08-08T20:16:52Z")

</div>

ah No. it says

not found: value $

not found: value value

---

<div class="post-metadata">

**Author:** ![Robert\_Bacon](https://avatars.discourse-cdn.com/v4/letter/r/278dde/32.png) [@Robert\_Bacon](https://community.gatling.io/u/Robert_Bacon)\
**Post date:** [August 8, 2017, 8:36pm UTC](https://community.gatling.io/t/hot-to-use-session-value-in-another-function-gatling/3873/10 "2017-08-08T20:36:59Z")

</div>

Are both of your objects under the same class? Have you tried setting val x = value without any of the extra symbols. Don’t think that will work, but I’m trying to figure out the logic behind this. My thoughts are either that or maybe single quotes ‘value’.

---

<div class="post-metadata">

**Author:** ![leela\_krishna](https://avatars.discourse-cdn.com/v4/letter/l/35a633/32.png) [@leela\_krishna](https://community.gatling.io/u/leela_krishna)\
**Post date:** [August 9, 2017, 1:08pm UTC](https://community.gatling.io/t/hot-to-use-session-value-in-another-function-gatling/3873/11 "2017-08-09T13:08:31Z")

</div>

Yes, both are in the same class. I am not sure why its not working for me ☹

---

<div class="post-metadata">

**Author:** ![Robert\_Bacon](https://avatars.discourse-cdn.com/v4/letter/r/278dde/32.png) [@Robert\_Bacon](https://community.gatling.io/u/Robert_Bacon)\
**Post date:** [August 9, 2017, 3:18pm UTC](https://community.gatling.io/t/hot-to-use-session-value-in-another-function-gatling/3873/12 "2017-08-09T15:18:24Z")

</div>

Ok Leela, I have a couple more ideas. Let’s try this…

Try calling your value like this:

val x: String = “${value}”

or like this:

val x = s"${value}"

let me know how that goes.

---

<div class="post-metadata">

**Author:** ![leela\_krishna](https://avatars.discourse-cdn.com/v4/letter/l/35a633/32.png) [@leela\_krishna](https://community.gatling.io/u/leela_krishna)\
**Post date:** [August 9, 2017, 3:46pm UTC](https://community.gatling.io/t/hot-to-use-session-value-in-another-function-gatling/3873/13 "2017-08-09T15:46:48Z")

</div>

thanks for the response Rob, I tried both earlier but none of them worked  
when

exec(session=\>session.set(“value”,session(“authHeader”).as[String]))  
.exec(test.dummy)

object test{  
def dummy: String = {  
val x: String = “${value}”  
println(x)  
return x  
}

I see this

TestScript is the only simulation, executing it.

${value}

When I use like this  
exec(session=\>session.set(“value”,session(“authHeader”).as[String]))  
.exec(test.dummy)

object test{  
def dummy: String = {  
val x = s"${value}"  
println(x)  
return x  
}

I see something like this,

not found: value value

11:43:00.080 [ERROR] i.g.c.ZincCompiler$ - val x = s"${value}"

I’m lot of ways but none of them are working 😑

---

<div class="post-metadata">

**Author:** ![Robert\_Bacon](https://avatars.discourse-cdn.com/v4/letter/r/278dde/32.png) [@Robert\_Bacon](https://community.gatling.io/u/Robert_Bacon)\
**Post date:** [August 9, 2017, 5:30pm UTC](https://community.gatling.io/t/hot-to-use-session-value-in-another-function-gatling/3873/14 "2017-08-09T17:30:53Z")

</div>

Ok Leela… I think I might have found the answer. When calling your “value” for ‘x’, surround it with 3 additional quotes, so it will look like this:

val x = “”""${value}""""

Let me know.

---

<div class="post-metadata">

**Author:** ![leela\_krishna](https://avatars.discourse-cdn.com/v4/letter/l/35a633/32.png) [@leela\_krishna](https://community.gatling.io/u/leela_krishna)\
**Post date:** [August 9, 2017, 6:05pm UTC](https://community.gatling.io/t/hot-to-use-session-value-in-another-function-gatling/3873/15 "2017-08-09T18:05:41Z")

</div>

I can see the value stored in the variable by using something like this

```auto
@volatile var val1 = ""

```

```auto
.exec(session=>{
  val1 = session("authHeader").as[String]
 println(val1)
  session
})

```

so, when I print val1, I can see the value. But when I try to use that in a separate function, Its again printing ${value}

I even Tried your suggestion. Like this …

```auto
 exec(session=>session.set("value",session("authHeader").as[String]))
  exec(test.dummy)

object test{
  def dummy: String = {
    val x = """"${value}""""
    println(x)
    return x
  }

}

```

Its just printing “${value}”

---

<div class="post-metadata">

**Author:** ![Robert\_Bacon](https://avatars.discourse-cdn.com/v4/letter/r/278dde/32.png) [@Robert\_Bacon](https://community.gatling.io/u/Robert_Bacon)\
**Post date:** [August 9, 2017, 6:11pm UTC](https://community.gatling.io/t/hot-to-use-session-value-in-another-function-gatling/3873/16 "2017-08-09T18:11:19Z")

</div>

Just making sure I saw correctly, in other parts of your code, you actually used .saveAs(“value”), correct?

---

<div class="post-metadata">

**Author:** ![leela\_krishna](https://avatars.discourse-cdn.com/v4/letter/l/35a633/32.png) [@leela\_krishna](https://community.gatling.io/u/leela_krishna)\
**Post date:** [August 9, 2017, 6:16pm UTC](https://community.gatling.io/t/hot-to-use-session-value-in-another-function-gatling/3873/17 "2017-08-09T18:16:07Z")

</div>

yes. I’m using the same name as I used for saveAs.

---

<div class="post-metadata">

**Author:** ![Robert\_Bacon](https://avatars.discourse-cdn.com/v4/letter/r/278dde/32.png) [@Robert\_Bacon](https://community.gatling.io/u/Robert_Bacon)\
**Post date:** [August 9, 2017, 7:24pm UTC](https://community.gatling.io/t/hot-to-use-session-value-in-another-function-gatling/3873/18 "2017-08-09T19:24:03Z")

</div>

Maybe that is confusing the compiler? Have you tried taking out the session.set portion?

---

<div class="post-metadata">

**Author:** ![leela\_krishna](https://avatars.discourse-cdn.com/v4/letter/l/35a633/32.png) [@leela\_krishna](https://community.gatling.io/u/leela_krishna)\
**Post date:** [August 9, 2017, 7:53pm UTC](https://community.gatling.io/t/hot-to-use-session-value-in-another-function-gatling/3873/19 "2017-08-09T19:53:52Z")

</div>

I tried multiple ways. How can we actually use the value which is saved using .saveAs(“dummy”)  
in another function or request. Do you have any idea about that?

---

<div class="post-metadata">

**Author:** ![Robert\_Bacon](https://avatars.discourse-cdn.com/v4/letter/r/278dde/32.png) [@Robert\_Bacon](https://community.gatling.io/u/Robert_Bacon)\
**Post date:** [August 9, 2017, 8:07pm UTC](https://community.gatling.io/t/hot-to-use-session-value-in-another-function-gatling/3873/20 "2017-08-09T20:07:51Z")

</div>

All I know is that I was struggling with it for a long time, but finally got it to work. Only problem is that the minor variances on your code and mine has extinguished any ideas I have as to why mine works and yours doesn’t. Everything else pretty much looks the same…

[Next page](https://community.gatling.io/t/hot-to-use-session-value-in-another-function-gatling/3873.md?page=2)
