# How to extract cookie Value

**URL:** <https://community.gatling.io/t/how-to-extract-cookie-value/824>\
**Category:** Gatling (Open-Source)\
**Created:** [February 18, 2014, 2:39pm UTC](https://community.gatling.io/t/how-to-extract-cookie-value/824 "2014-02-18T14:39:22Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alan\_Ryan](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@Alan\_Ryan](https://community.gatling.io/u/Alan_Ryan)\
**Post date:** [February 18, 2014, 2:39pm UTC](https://community.gatling.io/t/how-to-extract-cookie-value/824/1 "2014-02-18T14:39:22Z")

</div>

Hi,

I think I’m just not up to speed enough with Scala to figure this out (using 2.0.0-SNAPSHOT from about a week ago) . I have a call which returns with some cookie values set, I need to grab one of these values and store it for POST’ing later:

val chain\_2 = exec( http(“Get XTOK”)  
.get("""/portal/rest/contract/unassigned/count""")  
.check(status.is(200)))  
.exec( session =\> {

import io.gatling.http.cookie.\_  
import java.net.URI

session(“gatling.http.cookies”).validate[CookieJar].map{  
cookieJar =\>  
//val cookie = cookieJar.get(URI.create(“10.18.8.30”)).find(_.getName == “xtok”)//_.getName == “domain”)  
println(cookieJar)  
[//cookie.map](https://cookie.map)(session.set(“myCookieValue”, \_)).getOrElse(session)  
}

//println(session(“myCookieValue”));  
session  
})

this prints :

CookieJar(Map(10.18.8.30 → List(xtok=45695e2e-0a12-081e-00fe-b4052c4d6ed5; domain=10.18.8.30; path=/, JSESSIONID=pDRnTDnMs8v5Dw7GfBlBG62NnH2RrjqmKCR3kvGpvL9cyGtX53TS!-1606261601; domain=10.18.8.30; path=/; HTTPOnly)))

but I cannot figure out how to extract xtok from this - the code I have commented out throws :

[ERROR] [02/18/2014 14:37:59.050] [GatlingSystem-akka.actor.default-dispatcher-7] [akka://GatlingSystem/user/$d] null  
java.lang.NullPointerException  
at io.gatling.http.cookie.CookieJar$.io$gatling$http$cookie$CookieJar$$requestDomain(CookieJar.scala:29)

Thanks

Al

---

<div class="post-metadata">

**Author:** ![Excilys](https://avatars.discourse-cdn.com/v4/letter/e/8797f3/32.png) [@Excilys](https://community.gatling.io/u/Excilys)\
**Post date:** [February 18, 2014, 2:51pm UTC](https://community.gatling.io/t/how-to-extract-cookie-value/824/2 "2014-02-18T14:51:19Z")

</div>

You’re aware that Gatling will automatically send back to the server the suited cookies, right?  
Why don’t you trust the builtin behavior? Did you get a bug, or do you have some specific needs?

Your NPE is probably because “10.18.8.30” is an IP address, no a URI. “[http://10.18.8.30](http://10.18.8.30)” is.

You can bypass CookieJar logic and directly access the underlying Map:

val cookiesForDomain = cookieJar.store.get(“10.18.8.30”).getOrElse(Nil)

cookiesForDomain.filter(\_.getName == “xtok”).headOption match {  
case Some(xtok) =\> session =\> session.set(“myCookieValue”, xtok)  
case \_ =\> session  
}

---

<div class="post-metadata">

**Author:** ![Floris\_Kraak](https://avatars.discourse-cdn.com/v4/letter/f/848f3c/32.png) [@Floris\_Kraak](https://community.gatling.io/u/Floris_Kraak)\
**Post date:** [February 18, 2014, 2:59pm UTC](https://community.gatling.io/t/how-to-extract-cookie-value/824/3 "2014-02-18T14:59:13Z")

</div>

If I read his question right he wants to POST the cookie value into some form.  
As in, the server doesn’t read the posted cookie - it has a form field that expects a value that was put into a cookie value earlier.

Since I doubt gatling will replay the client javascript code that does that post normally this behaviour is pretty much expected.

---

<div class="post-metadata">

**Author:** ![Excilys](https://avatars.discourse-cdn.com/v4/letter/e/8797f3/32.png) [@Excilys](https://community.gatling.io/u/Excilys)\
**Post date:** [February 18, 2014, 3:02pm UTC](https://community.gatling.io/t/how-to-extract-cookie-value/824/4 "2014-02-18T15:02:51Z")

</div>

Get it. Thanks Floris!

---

<div class="post-metadata">

**Author:** ![Alan\_Ryan](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@Alan\_Ryan](https://community.gatling.io/u/Alan_Ryan)\
**Post date:** [February 18, 2014, 4:00pm UTC](https://community.gatling.io/t/how-to-extract-cookie-value/824/5 "2014-02-18T16:00:22Z")

</div>

Hi Floris/Stephane,

Yeah, I need to resubmit it later as a POST parameter… what ye suggested here works well for me (thanks for the Scala lessons !)

It looks like my cookie is returning

xtok=45aa30c4-0a12-081e-00fe-b4056416f582; domain=10.18.8.30; path=/

as a single value as opposed to three (key,value) pairs…so I think I need to work on the match…

Thanks,  
Al

---

<div class="post-metadata">

**Author:** ![Excilys](https://avatars.discourse-cdn.com/v4/letter/e/8797f3/32.png) [@Excilys](https://community.gatling.io/u/Excilys)\
**Post date:** [February 18, 2014, 4:20pm UTC](https://community.gatling.io/t/how-to-extract-cookie-value/824/6 "2014-02-18T16:20:31Z")

</div>

Which 3 key-value pairs are you expecting?

---

<div class="post-metadata">

**Author:** ![Alan\_Ryan](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@Alan\_Ryan](https://community.gatling.io/u/Alan_Ryan)\
**Post date:** [February 18, 2014, 4:35pm UTC](https://community.gatling.io/t/how-to-extract-cookie-value/824/7 "2014-02-18T16:35:01Z")

</div>

Hi, I was hoping to get just xtok, but I seem to get an entire string using this code: (Prints: “xtok=45aa30c4-0a12-081e-00fe-b4056416f582; domain=10.18.8.30; path=/”)

.exec(session=\>{  
import io.gatling.http.cookie.\_  
import java.net.URI  
session(“gatling.http.cookies”).validate[CookieJar].map {  
cookieJar =\>  
val cookiesForDomain = cookieJar.store.get(“10.18.8.30”).getOrElse(Nil)  
cookiesForDomain.filter(\_.getName == “xtok”).headOption match {  
case Some(xtok) =\>  
session.set(“myCookieValue”, xtok)  
case \_ =\> session  
}  
}  
}).exec(session=\>{  
println(session(“myCookieValue”).as[String])  
session  
})

Al

---

<div class="post-metadata">

**Author:** ![Excilys](https://avatars.discourse-cdn.com/v4/letter/e/8797f3/32.png) [@Excilys](https://community.gatling.io/u/Excilys)\
**Post date:** [February 18, 2014, 4:41pm UTC](https://community.gatling.io/t/how-to-extract-cookie-value/824/8 "2014-02-18T16:41:52Z")

</div>

If you want to just store the value, that’s session.set(“myCookieValue”, xtok.getRawValue)  
or getValue if you want it decoded (if you escaped some characters)

---

<div class="post-metadata">

**Author:** ![Alan\_Ryan](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@Alan\_Ryan](https://community.gatling.io/u/Alan_Ryan)\
**Post date:** [February 18, 2014, 4:49pm UTC](https://community.gatling.io/t/how-to-extract-cookie-value/824/9 "2014-02-18T16:49:16Z")

</div>

Great I’ll try that.

Thanks,

---

<div class="post-metadata">

**Author:** ![Alan\_Ryan](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@Alan\_Ryan](https://community.gatling.io/u/Alan_Ryan)\
**Post date:** [February 18, 2014, 4:52pm UTC](https://community.gatling.io/t/how-to-extract-cookie-value/824/10 "2014-02-18T16:52:21Z")

</div>

perfect 😉
