How to get value from Cookie returned by server

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! :slight_smile:

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

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)

})

One question: why do you want to access the cookies in the first place?

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!

@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

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