# how do i set data into session and use session data in my request

**URL:** https://community.gatling.io/t/how-do-i-set-data-into-session-and-use-session-data-in-my-request/1686
**Category:** Gatling (Open-Source)
**Created:** [November 13, 2014, 9:26am UTC](https://community.gatling.io/t/how-do-i-set-data-into-session-and-use-session-data-in-my-request/1686 "2014-11-13T09:26:00Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Raja\_Thumati](https://avatars.discourse-cdn.com/v4/letter/r/f475e1/32.png) [@Raja\_Thumati](https://community.gatling.io/u/Raja_Thumati)
#### Post date: [November 13, 2014, 9:26am UTC](https://community.gatling.io/t/how-do-i-set-data-into-session-and-use-session-data-in-my-request/1686/1 "2014-11-13T09:26:00Z")

</div>

Hi Stephene,

help me please,

i have some lines of code which is useful to store some data into session,

but i am unable to store data into session,

and based on the session data,i request for some other data through url like bellow (in request processing there i am using session data) ,

val dataVo=voMethod.getVos()val compData=new HashMapList[String],List[String]  
compData.put(dataVo.get(0),dataVo.get(1))

val dataKey=100

val scn2=scenario("").exec(session =\> {  
import io.gatling.http.cookie.\_  
import java.net.URI  
val newSession = (session(“gatling.http.cookies”).asOption[CookieJar].flatMap { cookieJar =\>  
val cookie = cookieJar.get(URI.create("[")).find(\_.getName == compData)

**cookie.map(session.set(“Key”+dataKey,com\*\*\*\*pData)) ------------------------------------------------------------------------------------------i am getting error here while set data into session**

}).getOrElse(session)  
newSession  
})

val httpProtocol = http  
.baseURL(“url”)  
.disableFollowRedirect  
.acceptHeader(“text/html,application/xhtml+xml,application/xml;q=0.9,_/_;q=0.8”)  
.doNotTrackHeader(“1”)  
.acceptLanguageHeader(“en-US,en;q=0.5”)  
.userAgentHeader(“Mozilla/5.0 (Windows NT 6.1; rv:32.0) Gecko/20100101 Firefox/32.0”)  
.disableResponseChunksDiscarding  
.connection(“keep-alive”)

val scn = scenario(“scenario”)  
.exec(http(“GETdata”)  
.get(dataKey+“geturl”))

setUp(scn.inject(atOnce(1 user))).protocols(httpProtocol)

---

<div class="post-metadata">

### Author: ![jarrowwx](https://avatars.discourse-cdn.com/v4/letter/j/4af34b/32.png) [@jarrowwx](https://community.gatling.io/u/jarrowwx)
#### Post date: [November 13, 2014, 3:51pm UTC](https://community.gatling.io/t/how-do-i-set-data-into-session-and-use-session-data-in-my-request/1686/2 "2014-11-13T15:51:05Z")

</div>

Your code won’t work. You can’t do the session.set() inside of the cookie map. Not like that, anyway.

Instead, you need to define a “var newSession = session” and where you do the assignment, do “newSession = newSession.set( … )” and at the end, return newSession.

The reason is that a session is immutable. When you call .set() it is returning a new object. Calling set over and over is creating many new objects, rather than doing what you want, which is adding many things to the one session object.
