Hot to use session value in another function gatling

I’m using Intelli J as my IDE. Do you import any libraries to your project?

http://gatling.io/docs/current/session/expression_el/#expression-language

Warning

This Expression Language only works on the final value that is passed to the DSL method when the Simulation is instantiated.

For example, queryParam("latitude", "${latitude}".toInt + 24) won’t work, the program will blow on "${latitude}".toInt as this String can’t be parsed into an Int.

The solution here would be to pass a function:

session => session("latitude").validate[Int].map(i => i + 24).

I’m trying to get the session value Like this …

  .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
  }

}

But I’m just seeing this in my console

Device is the only simulation, executing it.

${value}

Simulation Device started…

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

I’ve imported import io.gatling.core.Predef._
But still, I can’t grab the session value

Hey Rob, Stephane,
so, I’m saving my value as
val scn = scenario(“Device”)
.feed(csvFeeeder)
.exec(http(“Auth Header generation”)
.post("/auth")
.headers(headers_0)
.formParam(“type”, “device”)
.check(headerRegex(“Auth”,“Device*”).saveAs(“authHeader”))

So, I’m trying to use that authHeader value in my second request. For that I’m trying to use the session to get the value. But I failed to get that.
Please help me with this.

Leela,

When you call authHeader, did you use the session => session(“authHeader”).validate[String] syntax?

Potentially try:

session =>{
session(“authHeader”).validate[String]
session}

I did not try that. But where do I need to use that?

That would be used where you try to call authHeader after you have used .saveAs

I used that, But I don’t understand, how can I fetch the session value to my next .exec request. I think that session block will just validate my .saveAs. Correct me if I’m wrong Rob. I could not find a way to use my .saveAs value in my second request.

or Do you want me to assign this block

session =>{
session(“authHeader”).validate[String]
session}

to any kind val?

I’m still unable to parse the session value stored in one request into other.
I’ve two http requests
val scn = scenario(“Device”)
.feed(csvFeeeder)
.exec(http(“Auth Header gene)
.post(”/auth")
.headers(headers_0)
.formParam(“type”, “device”)
.check(headerRegex(“auth”,“device*”).saveAs(“authH”))
.check(status.is(401)))

.exec(http(“Signed Req”)
.post("/auth/oauth")
.headers(Map(
“x-id” → “${id}”,
“x-serial” → “${serial}”,
“Authorization” → test.dummy))
.formParam(“type”, “device”))

the test.dummy is a function which converts the saved string “authH” into some format and that converted output needs to be passed to the “Authorozation” header in my second request. for that purpose I’m using something like this

object test{
def dummy: String = {
val = (Here I need to get the saved authH session attribute)
— logic to convert into my required format–
return convertedString
}
}

I was unable to get the sessionAttribute there. Is there a way to get the sessionAttribute. Please HELP. I go through the gatling documentation but could not find any solution