# Only 1 cookie sent to server - even when multiple cookies are set in session

**URL:** <https://community.gatling.io/t/only-1-cookie-sent-to-server-even-when-multiple-cookies-are-set-in-session/2903>\
**Category:** Gatling (Open-Source)\
**Created:** [April 5, 2016, 10:37pm UTC](https://community.gatling.io/t/only-1-cookie-sent-to-server-even-when-multiple-cookies-are-set-in-session/2903 "2016-04-05T22:37:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anil\_Allewar](https://avatars.discourse-cdn.com/v4/letter/a/7ea924/32.png) [@Anil\_Allewar](https://community.gatling.io/u/Anil_Allewar)\
**Post date:** [April 5, 2016, 10:37pm UTC](https://community.gatling.io/t/only-1-cookie-sent-to-server-even-when-multiple-cookies-are-set-in-session/2903/1 "2016-04-05T22:37:54Z")

</div>

Gatling version: 2.1.7

The server application that I am testing needs 2 cookies to validate the authenticity of logged in user. I do get the 2 cookies when I log-in and then I initiate another request by setting the header values on Cookies; however on the server I always get only 1 cookie, the “SecurityTokenURL”.

Is there something that I am missing or doing wrong?

`  
package com.xxx

import io.gatling.core.Predef.\_  
import io.gatling.http.Predef.\_

class TestSimulation extends Simulation {

val httpProtocol = http  
.baseURL(“[http://xxxx-server/](http://xxxx-server/)”)  
.disableCaching  
.inferHtmlResources(BlackList("""._.js""", “”"._.css""", “”"._.gif""", “”"._.jpeg""", “”"._.jpg""", “”"._.ico""", “”"._.woff""", “”"._.(t|o)tf""", “”"._.png""", “”"._.html"""), WhiteList())

val KEY\_CENTRIC\_REQUEST\_PREVENT\_CACHE = “request.preventCache”;

val CONNECTION\_HEADER\_VALUE = “keep-alive”;

val commonHeaders = Map(  
HttpHeaderNames.Accept → HttpHeaderValues.ApplicationJson,  
HttpHeaderNames.CacheControl → HttpHeaderValues.NoCache,  
HttpHeaderNames.Connection → CONNECTION\_HEADER\_VALUE)

val randomGenerator = new scala.util.Random(99999999)

val scn = scenario(“Login”)  
.exec(  
http(“login”)  
.post("/csi-requesthandler/RequestHandler")  
.headers(commonHeaders)  
.header(HttpHeaderNames.ContentType, HttpHeaderValues.MultipartFormData)  
.queryParam(KEY\_CENTRIC\_REQUEST\_PREVENT\_CACHE, randomGenerator.nextInt())  
.formParamMap(Map(“LoginID” → “xxx”,  
“Password” → “xxx”,  
“Module” → “xxx”,  
“Operation” → “SimpleLogin”,  
“OutputJSON” → “1”))  
.check(  
headerRegex(HttpHeaderNames.SetCookie, “SecurityTokenURL=[\w:/]+”)  
.transform(header =\> extractAndTransformHeader(header))  
.saveAs(“securityTokenAuthHeader”))  
.check(  
headerRegex(HttpHeaderNames.SetCookie, “JSESSIONID=[\w:/]+”)  
.transform(header =\> extractAndTransformHeader(header))  
.saveAs(“jsessionIdAuthHeader”))  
.check(regex(""“Status:“Successful”,”"")))

val getCurrentSessionScn = scenario(“GetCurrentSession”)  
.exec(session =\> {  
flushSessionCookies  
val securityTokenArray = session(“securityTokenAuthHeader”).as[Array[String]]  
println(“securityTokenArray: " + securityTokenArray(0) + “:” + securityTokenArray(1))  
addCookie(Cookie(securityTokenArray(0), securityTokenArray(1)).withPath(”/"))

val sessionIdArray = session(“jsessionIdAuthHeader”).as[Array[String]]  
println(“sessionIdArray: " + sessionIdArray(0) + “:” + sessionIdArray(1))  
addCookie(Cookie(sessionIdArray(0), sessionIdArray(1)).withPath(”/csi-requesthandler"))

session

})  
.exec(http(“getcurrentsession”)  
.get("/csi-requesthandler/RequestHandler")  
.headers(commonHeaders)  
.queryParamMap(Map(KEY\_CENTRIC\_REQUEST\_PREVENT\_CACHE → randomGenerator.nextInt(),  
“Module” → “xxx”,  
“Operation” → “GetCurrentSession”,  
“OutputJSON” → “1”))  
.check(regex(""“Status:“Successful”,”"")  
.transform(body =\> extractAndTransformHeader(body))))

val allScenarios = scn.exec(getCurrentSessionScn)

setUp(  
allScenarios.inject(atOnceUsers(2))).protocols(httpProtocol)

def extractAndTransformHeader(originalHeader: String): Array[String] = {  
originalHeader.split("=")  
}

}  
`

---

<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:** [April 6, 2016, 7:06pm UTC](https://community.gatling.io/t/only-1-cookie-sent-to-server-even-when-multiple-cookies-are-set-in-session/2903/2 "2016-04-06T19:06:43Z")

</div>

You can’t use Gatling DSL components inside functions.  
Those are merely builders.

---

<div class="post-metadata">

**Author:** ![Anil\_Allewar](https://avatars.discourse-cdn.com/v4/letter/a/7ea924/32.png) [@Anil\_Allewar](https://community.gatling.io/u/Anil_Allewar)\
**Post date:** [April 7, 2016, 10:44pm UTC](https://community.gatling.io/t/only-1-cookie-sent-to-server-even-when-multiple-cookies-are-set-in-session/2903/3 "2016-04-07T22:44:36Z")

</div>

Thanks for the pointer. For those who are interested, this is how I achieved it

`  
val getCurrentSessionScn = scenario(“GetCurrentSession”)  
.exec(session =\> {  
val securityTokenArray = session(“securityTokenAuthHeader”).as[Array[String]]  
val sessionIdArray = session(“jsessionIdAuthHeader”).as[Array[String]]

session.set(“SecurityTokenURLValue”,securityTokenArray(1)).set(“JSESSIONIDValue”, sessionIdArray(1))  
})  
.exec(  
addCookie(Cookie(“SecurityTokenURL”, “${SecurityTokenURLValue}”))  
)  
.exec(  
addCookie(Cookie(“JSESSIONID”, “${JSESSIONIDValue}”))  
)  
.exec(http(“getcurrentsession”)  
.get("/csi-requesthandler/RequestHandler")  
.headers(commonHeaders)  
.queryParamMap(Map(KEY\_CENTRIC\_REQUEST\_PREVENT\_CACHE → randomGenerator.nextInt(),  
“Module” → “xxx”,  
“Operation” → “GetCurrentSession”,  
“OutputJSON” → “1”))  
.check(regex(""“Status:“Successful”,”""))  
)  
`

Thanks,

Anil

---

<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:** [April 8, 2016, 6:10am UTC](https://community.gatling.io/t/only-1-cookie-sent-to-server-even-when-multiple-cookies-are-set-in-session/2903/4 "2016-04-08T06:10:32Z")

</div>

Too much boilerplate. Gatling EL can access array elements by index: [http://gatling.io/docs/2.1.7/session/expression\_el.html#expression-language](http://gatling.io/docs/2.1.7/session/expression_el.html#expression-language)
