How to extract cookie Value

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(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

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

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.

Get it. Thanks Floris!

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

Which 3 key-value pairs are you expecting?

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

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)

Great I’ll try that.

Thanks,

perfect :wink: