# How to get that pesky session data and call a http function inside exec

**URL:** <https://community.gatling.io/t/how-to-get-that-pesky-session-data-and-call-a-http-function-inside-exec/2110>\
**Category:** Gatling (Open-Source)\
**Created:** [March 12, 2015, 11:16pm UTC](https://community.gatling.io/t/how-to-get-that-pesky-session-data-and-call-a-http-function-inside-exec/2110 "2015-03-12T23:16:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rreynolds](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@rreynolds](https://community.gatling.io/u/rreynolds)\
**Post date:** [March 12, 2015, 11:16pm UTC](https://community.gatling.io/t/how-to-get-that-pesky-session-data-and-call-a-http-function-inside-exec/2110/1 "2015-03-12T23:16:43Z")

</div>

I am trying to call a custom function to generate a map of headers, then use that map to pass to the http.headers() function. However, while attempting this in the body of an exec method, the test runs, but doesn’t execute the actual GET call.

Perhaps there is a better what to do this altogether. Thanks in advance for any help.

`

class RecordedSimulation extends Simulation {

val httpProtocol = http  
.inferHtmlResources()  
.acceptEncodingHeader(“gzip, identity”)  
.connection(“Keep-Alive, TE”)  
.contentTypeHeader(“application/json”)  
.userAgentHeader(“Gatling”)

val director = “[http://google.com](http://google.com)”

var feeder = Iterator.continually(Map(“deviceId” → CreateDeviceId()))

val scn = scenario(“RecordedSimulation”)  
.feed(feeder)  
.exec(session =\>  
{  
val headers = CreateHeader(“test”, session(“deviceId”).as[String])  
http(“Director”)  
.get(director)  
.headers(headers)  
.check(status.is(200))  
.check(jsonPath("$.serverUrl").saveAs(“serverUrl”))  
session  
})

setUp(scn.inject(atOnceUsers(1))).protocols(httpProtocol)  
}

`

CreateHeader is from an imported class ( if it matters ):

`

def CreateHeader(time : String, deviceId : String): Map[String, String] = {

val headers = Map(  
“TE” → “identity”,  
“deviceId” → deviceId,  
“time” → time,  
“token” → GetTokenHeaderValue(time, deviceId))  
return headers;  
}

`

Test results: ( i have tracing enabled in logback config )

GATLING\_HOME is set to /usr/local/gatling/2.1.4  
Simulation xxx.RecordedSimulation started…  
Creds: 4833b584-3098-4486-868f-d8a49e8b50e5:test

---

<div class="post-metadata">

**Author:** ![Abhinav\_Gogna](https://avatars.discourse-cdn.com/v4/letter/a/c0e974/32.png) [@Abhinav\_Gogna](https://community.gatling.io/u/Abhinav_Gogna)\
**Post date:** [March 13, 2015, 7:59pm UTC](https://community.gatling.io/t/how-to-get-that-pesky-session-data-and-call-a-http-function-inside-exec/2110/2 "2015-03-13T19:59:58Z")

</div>

How about capturing headers in session function first and then calling get. I haven’t done anything like this before but based on my limited understanding it should work, I hope.

`  
val scn = scenario(“RecordedSimulation”)  
.feed(feeder)  
.exec{session =\>  
val headers = CreateHeader(“test”, session(“deviceId”).as[String])  
session.set(“headers”, headers)  
}  
.exec(http(“Director”)  
.get(director)  
.headers("${headers}")  
.check([status.is](http://status.is)(200))  
.check(jsonPath("$.serverUrl").saveAs(“serverUrl”))  
)

`

---

<div class="post-metadata">

**Author:** ![rreynolds](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@rreynolds](https://community.gatling.io/u/rreynolds)\
**Post date:** [March 13, 2015, 8:56pm UTC](https://community.gatling.io/t/how-to-get-that-pesky-session-data-and-call-a-http-function-inside-exec/2110/3 "2015-03-13T20:56:32Z")

</div>

Good suggestion, which led me to a solution. Unfortunately, headers() is not one of the functions that accepts gatling’s EL. Fortunately, header() does, and I was able to do this:

`

val scn = scenario(“RecordedSimulation”)

.feed(feeder)  
.exec(http(“Director”)  
.get(director)  
.check(status.is(200))  
.check(jsonPath("$.serverUrl").saveAs(“serverUrl”)))  
.pause(1) // 10  
**.exec(session =\> SetHeaderValuesInSession(session))**  
.exec(http(“Register”)  
.get(“https://${serverUrl}/xxx/xxx/xxx”)  
**.header(“time”,"${time}")**  
**.header(“deviceId”,"${deviceId}")**  
**.header(“token”,"${token}")**  
.check(status.is(200))  
.check(jsonPath("$.simsId").saveAs(“simsId”)))  
.exec(session =\> {  
println(session)  
session  
})

setUp(scn.inject(atOnceUsers(1))).protocols(httpProtocol)

def SetHeaderValuesInSession(session : Session): Session = {  
val time = GetTime()  
val deviceId = session(“deviceId”).as[String]  
val token = GetTokenHeaderValue(time, deviceId)  
**session.set(“time”,time).set(“token”, token)**  
}

`

Thanks again!

-Ryan
