How to get value from Cookie returned by server

2,787 views
Skip to first unread message

Jordi Medina Andreu

unread,
Jul 11, 2016, 8:14:12 AM7/11/16
to Gatling User Group

Hi there,

I'm trying to get value from a cookie returned by the server in header to a GET request. I would like to use such value to next request (POST).

So far I was focused to print and save the value from the cookie, but I'm not able because uses CookieKey and StoredCookie objects and despite I read some of the email chains from the group, I couldn't find the solution to my problem.
Some support would be really appreciated!  :)



val allSteps = 
exec(getRequestTime)
 
.exec( session => {

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


      session("gatling.http.cookies").validate[CookieJar].map{
      cookieJar =>
println(cookieJar)
}
    session
  }) 
  
  
  
The result of the println(cookieJar),  is:
  
CookieJar(Map(
CookieKey(bwcfst,cc-test.sagecat.es,/cc/time) -> StoredCookie(BWCFST=wYTtx6HUbUPsUDMiwzBBmgaxKJqWcMn0JDd5lqLWBLA; path=/catc/time; secure; HTTPOnly,true,false,1468232405357), 
CookieKey(bwclocale,sagecat.es,/) -> StoredCookie(bwCurrentLocale=en_US; domain=.sagecat.es; path=/,false,false,1468232405357), 
CookieKey(jsessionid,catc-test.sagecat.es,/cc/time) -> StoredCookie(JSESSIONID=vSxmpOMTLphT8SyOSMs9ScMwGMBlUV3Ku2IvdhKm.cca; path=/catc/time; secure; HTTPOnly,true,false,1468232405357), 
CookieKey(jsessionid,catc-test.sagecat.es,/cc/access) -> StoredCookie(JSESSIONID=V8Xk36hK5ocnDZ9YSwOgggm_xaMYPhhzbJ7SW38o.cca; path=/catc/access; secure; HTTPOnly,true,false,1468232405357), 
CookieKey(csrftoken,sagecat.es,/) -> StoredCookie(csrftoken=1877ad7e80abfb99442d736495a3aa89; domain=.sagecat.es; path=/; secure,false,false,1468232405357)
))


I would like to get value from 'csrftoken' (i.e: 1877ad7e80abfb99442d736495a3aa89), and store in session in order to be used by next request (POST request)
any ideas, please?


thanks a lot!!

Jordi Medina

unread,
Jul 12, 2016, 9:20:08 AM7/12/16
to Gatling User Group
Finally I find the way to get the value from the cookie. I'm sharing here:

.exec(session => {
import io.gatling.http.cookie._
        
val ck = session("gatling.http.cookies").as[CookieJar].get(org.asynchttpclient.uri.Uri.create("https://sagecat.es")).find(_.getName == "csrftoken")
//println("X-CSRFToken: " + ck.getOrElse(null).getValue)

session.set("myCookieValue", ck.getOrElse(null).getValue)
})

Stéphane LANDELLE

unread,
Jul 13, 2016, 2:34:45 AM7/13/16
to gat...@googlegroups.com
One question: why do you want to access the cookies in the first place?

Stéphane Landelle
GatlingCorp CEO


--
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/d/optout.

ph

unread,
Oct 30, 2018, 9:42:02 AM10/30/18
to Gatling User Group
Thank you very much for sharing this!

@Stéphane Landelle: the need to get/validate token does exist. In my case, the XSRF token is modified and the following POST requests have to be manually tuned. 

Also my approach is shared as follows, with a regex to extract the token:

val URL = "http://example.com"
val XSRF_COOKIE_NAME = "XSRF-TOKEN"
val cookiePattern = "[A-Za-z-]+=([A-Za-z0-9-]*).*".r  // "xsrf-token=abcd-efgh-ijkl; path=/"
...

exec(session => {
   val token = session("gatling.http.cookies").as[CookieJar]
    .get(Uri.create(URL))
    .find(_.getName == XSRF_COOKIE_NAME) match {
      case None => ""
      case Some(cookie) => match {
        cookie.toString match {
          case cookiePattern(token) => token
          case _ = ""
        }
      }
    }
    session.set("xsrfToken", token)
})
.exec(http("A following request with latest token")
  .post("http://foo.com")
  .header("X-XSRF_TOKEN", "${xsrfToken}")
  .check(status.in(200))
)


Enjoy!

Stéphane LANDELLE

unread,
Oct 30, 2018, 10:17:38 AM10/30/18
to gat...@googlegroups.com
@pythonhicom What you're doing is very wrong. You're hacking a Gatling internal and will be sorry if we change it someday.
So what you're doing is definitively NOT A RECOMMENDED SOLUTION.

There's a built-in for extracting a cookie value from the cookie jar, please use the official way: https://gatling.io/docs/current/http/http_helpers/?highlight=getcookievalue#getting-a-cookie-value


Stéphane Landelle
GatlingCorp CTO



--

Stef Joosten

unread,
Dec 23, 2021, 8:27:37 AM12/23/21
to Gatling User Group
Hi Jordi, glad you made this work. I am wrestling with exactly the same problem for three days already without any luck.
I tried your solution too, but I cannot get your solution to work for me. I get an error message: class CookieJar in package cookie cannot be accessed as a member of package io.gatling.http.cookie
Maybe something has changed since you wrote this. Do you have any idea why I get this message or how to make it work?
Thank you in advance!
Stef Joosten

Op dinsdag 30 oktober 2018 om 15:17:38 UTC+1 schreef Stéphane Landelle:
Reply all
Reply to author
Forward
0 new messages