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( session => {

import io.gatling.http.cookie._



cookieJar =>





The result of the println(cookieJar), is:

CookieKey(bwcfst,,/cc/time) → StoredCookie(BWCFST=wYTtx6HUbUPsUDMiwzBBmgaxKJqWcMn0JDd5lqLWBLA; path=/catc/time; secure; HTTPOnly,true,false,1468232405357),
CookieKey(bwclocale,,/) → StoredCookie(bwCurrentLocale=en_US;; path=/,false,false,1468232405357),
CookieKey(jsessionid,,/cc/time) → StoredCookie(JSESSIONID=vSxmpOMTLphT8SyOSMs9ScMwGMBlUV3Ku2IvdhKm.cca; path=/catc/time; secure; HTTPOnly,true,false,1468232405357),
CookieKey(jsessionid,,/cc/access) → StoredCookie(JSESSIONID=V8Xk36hK5ocnDZ9YSwOgggm_xaMYPhhzbJ7SW38o.cca; path=/catc/access; secure; HTTPOnly,true,false,1468232405357),
CookieKey(csrftoken,,/) → StoredCookie(csrftoken=1877ad7e80abfb99442d736495a3aa89;; 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(“”)).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 = “
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]
.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”)
.header(“X-XSRF_TOKEN”, “${xsrfToken}”)


@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:

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