How to extract cookie Value

2,589 views
Skip to first unread message

Alan Ryan

unread,
Feb 18, 2014, 9:39:22 AM2/18/14
to gat...@googlegroups.com
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

Stéphane Landelle

unread,
Feb 18, 2014, 9:51:19 AM2/18/14
to gat...@googlegroups.com
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
}


--
You received this message because you are subscribed to the Google Groups "Gatling User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gatling+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Floris Kraak

unread,
Feb 18, 2014, 9:59:13 AM2/18/14
to gat...@googlegroups.com
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.
--
REALITY.SYS corrupted. Reboot Universe? (Y/N/Q)

Stéphane Landelle

unread,
Feb 18, 2014, 10:02:51 AM2/18/14
to gat...@googlegroups.com
Get it. Thanks Floris!

Alan Ryan

unread,
Feb 18, 2014, 11:00:22 AM2/18/14
to gat...@googlegroups.com
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






--
You received this message because you are subscribed to a topic in the Google Groups "Gatling User Group" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/gatling/OWVVoWMsFd0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to gatling+u...@googlegroups.com.

Stéphane Landelle

unread,
Feb 18, 2014, 11:20:31 AM2/18/14
to gat...@googlegroups.com
Which 3 key-value pairs are you expecting?

Alan Ryan

unread,
Feb 18, 2014, 11:35:01 AM2/18/14
to gat...@googlegroups.com
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


Stéphane Landelle

unread,
Feb 18, 2014, 11:41:52 AM2/18/14
to gat...@googlegroups.com
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)

Alan Ryan

unread,
Feb 18, 2014, 11:49:16 AM2/18/14
to gat...@googlegroups.com
Great I'll try that.

Thanks,

Alan Ryan

unread,
Feb 18, 2014, 11:52:21 AM2/18/14
to gat...@googlegroups.com
perfect ;-)
Reply all
Reply to author
Forward
0 new messages