# Cookie handling issues

**URL:** <https://community.gatling.io/t/cookie-handling-issues/7368>\
**Category:** Gatling (Open-Source)\
**Tags:** gatling-academy\
**Created:** [December 1, 2022, 10:51am UTC](https://community.gatling.io/t/cookie-handling-issues/7368 "2022-12-01T10:51:03Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![balaji](https://avatars.discourse-cdn.com/v4/letter/b/2bfe46/32.png) [@balaji](https://community.gatling.io/u/balaji)\
**Post date:** [December 1, 2022, 10:51am UTC](https://community.gatling.io/t/cookie-handling-issues/7368/1 "2022-12-01T10:51:03Z")

</div>

I m using intellij for gatling script developments using scala.  
while handling cookies im seeing this error

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/gatling2/original/2X/e/ec1a648a69abdde1ce90cfc88213a71e6ed7d406.png)

Not found: type CookieJar. How to fix this?

---

<div class="post-metadata">

**Author:** ![sbrevet](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/sbrevet/32/830_2.png) [@sbrevet](https://community.gatling.io/u/sbrevet)\
**Post date:** [December 1, 2022, 11:21am UTC](https://community.gatling.io/t/cookie-handling-issues/7368/2 "2022-12-01T11:21:21Z")

</div>

Hi @balaji,

Why do you need to access this session variable?

Did you read the [documentation about cookies](https://gatling.io/docs/gatling/reference/current/http/helpers/#dealing-with-cookies)?

Do you need something else? What is your use case?

Cheers!

---

<div class="post-metadata">

**Author:** ![balaji](https://avatars.discourse-cdn.com/v4/letter/b/2bfe46/32.png) [@balaji](https://community.gatling.io/u/balaji)\
**Post date:** [December 1, 2022, 11:36am UTC](https://community.gatling.io/t/cookie-handling-issues/7368/3 "2022-12-01T11:36:38Z")

</div>

Hi  
Thanks for the reply.  
This is my use case

1. I am using a POST saml request (https://\<hostname.domain\>/acmserver/api/saml) with some form params and in response it sets a cookie. attached screenshot  
 ![image](https://europe1.discourse-cdn.com/flex013/uploads/gatling2/original/2X/7/7340c163b866fd2a421e57853dc6ba3b12028f11.png)

I need to get the value of acmToken cookie? I tried cookie options in documentation but im getting cookie not found error.

---

<div class="post-metadata">

**Author:** ![slandelle](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/slandelle/32/4_2.png) [@slandelle](https://community.gatling.io/u/slandelle)\
**Post date:** [December 1, 2022, 1:06pm UTC](https://community.gatling.io/t/cookie-handling-issues/7368/4 "2022-12-01T13:06:16Z")

</div>

As visible in your screenshot, this class is private, not for end users to use. Use [Gatling - HTTP Helpers](https://gatling.io/docs/gatling/reference/current/http/helpers/) instead.

---

<div class="post-metadata">

**Author:** ![balaji](https://avatars.discourse-cdn.com/v4/letter/b/2bfe46/32.png) [@balaji](https://community.gatling.io/u/balaji)\
**Post date:** [December 1, 2022, 1:42pm UTC](https://community.gatling.io/t/cookie-handling-issues/7368/5 "2022-12-01T13:42:40Z")

</div>

Yes when i used it shows cookie not available error

```scala
  val saml = scenario("ACM Token")
    .exec(http("SP")
      // Define SP's URL.
      .post("https://xxxxxx/server/api/saml")
      .header("Content-Type", "application/x-www-form-urlencoded")
      .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36")
      .header("Accept-Encoding", "gzip, deflate, br")
      .formParam("RelayState", Configuration.relayState)
      .formParam("SAMLResponse", Configuration.saml)
  )
  .exec {
    session =>
      println(session)
      println("attributessss" + session.attributes)

     
      println( session.attributes.get("gatling.http.cookies"))
      println("xxxxx" + getCookieValue(CookieKey("acmToken")))

      session
  }

```

output

```auto
Some(CookieJar(Map(CookieKey(acmtoken,uat.dal-stage.flatironscloud.com,/) -> StoredCookie(acmToken=eyJhbGciOiJSUzI1NiJ9.eyJzdWIiOiIxNzA3NzY3IiwiZmlyc3RuYW1lIjoiQ3JhaWciLCJuY2FnZSI6IjdBMzQ0Iiwicm9sZXMiOlsiRXh0ZXJuYWwgVmlld2VyIHJvbGUiLCJUZGRBQ01BZG1pbkRldiByb2xlIiwiVGRkUGluUG9pbnRQdWJNYW51YWxzRGV2IHJvbGUiLCJUc2REYXRhTWdyUmVsZWFzTWFudWFsRGV2IHJvbGUiXSwiaXNzIjoiaHR0cHM6XC9cL2ZsYXRpcm9uc3NvbHV0aW9ucy5jb20iLCJncm91cHMiOlsiVGRkUGluUG9pbnRWaWV3ZXJEZXYiLCJBVExfUkNDVFNVUkwwNDQyIiwiVGRkUGluUG9pbnRQdWJNYW51YWxzRGV2IiwiVHNkRGF0YU1nclJlbGVhc01hbnVhbERldiIsIlRkZEFDTUFkbWluRGV2Il0sImV4cCI6MTY2OTk4ODM3MiwiaWF0IjoxNjY5OTAxOTcyLCJsYXN0bmFtZSI6Ik1jTnV0dCIsImFwcHMiOlsicHAiLCJkbSIsImFjbSIsImFtIl19.RHfv-qALc18qeX_-QYzWYTiRCaDfXFlEJu2PE7LZckrdSIJxZwj2veYofKMKIKsk9vHUI6LpaJkRc0TdKnlQ37YW87kLm42OrIMMu-EGjiPy5VexCOqfQDkmxdAcGRzhgGGqCwcDw6GshU2GVuD7rnLqCYobXGdLhhWajUq25NY, path=/,true,false,1669901972483))))
xxxxxio.gatling.http.action.cookie.GetCookieBuilder@91e3e5b

```

I am not able to get this acmtoken value  
acmToken=\<\>

---

<div class="post-metadata">

**Author:** ![sbrevet](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/sbrevet/32/830_2.png) [@sbrevet](https://community.gatling.io/u/sbrevet)\
**Post date:** [December 1, 2022, 2:26pm UTC](https://community.gatling.io/t/cookie-handling-issues/7368/6 "2022-12-01T14:26:16Z")

</div>

`getCookieValue(...)` is a step (ie, it has to be run in `exec` block)

```scala
  val saml = scenario("ACM Token")
    .exec(http("SP")
      // Define SP's URL.
      .post("https://xxxxxx/server/api/saml")
      .header("Content-Type", "application/x-www-form-urlencoded")
      .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36")
      .header("Accept-Encoding", "gzip, deflate, br")
      .formParam("RelayState", Configuration.relayState)
      .formParam("SAMLResponse", Configuration.saml)
    )
    .exec(getCookieValue(CookieKey("acmToken")))
    .exec { session =>
      println(session("acmToken").as[String])
    }

```

---

<div class="post-metadata">

**Author:** ![slandelle](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/slandelle/32/4_2.png) [@slandelle](https://community.gatling.io/u/slandelle)\
**Post date:** [December 1, 2022, 3:01pm UTC](https://community.gatling.io/t/cookie-handling-issues/7368/7 "2022-12-01T15:01:12Z")

</div>

As shown in the documentation, getCookie is an Action that must be directly passed to exec, not something you can invoke inside a function.

---

<div class="post-metadata">

**Author:** ![balaji](https://avatars.discourse-cdn.com/v4/letter/b/2bfe46/32.png) [@balaji](https://community.gatling.io/u/balaji)\
**Post date:** [December 1, 2022, 3:26pm UTC](https://community.gatling.io/t/cookie-handling-issues/7368/8 "2022-12-01T15:26:20Z")

</div>

> [@sbrevet](#):
>
> ```auto
> .exec(getCookieValue(CookieKey("acmToken")))
> .exec { session =>
> println(session("acmToken").as[String])
> }
> 
> ```

I am getting this error  
7  
Select run description (optional)

Simulation perf.search.samlexample started…  
20:55:45.976 [ERROR] i.g.h.a.c.GetCookieBuilder$$anon$1 - ‘getCookie-2’ failed to execute: Neither cookie domain nor baseUrl nor wsBaseUrl  
20:55:45.988 [ERROR] i.g.c.a.b.SessionHookBuilder$$anon$1 - ‘hook-1’ crashed with ‘j.u.NoSuchElementException: No attribute named ‘acmToken’ is defined’, forwarding to the next one

======

---

<div class="post-metadata">

**Author:** ![slandelle](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/slandelle/32/4_2.png) [@slandelle](https://community.gatling.io/u/slandelle)\
**Post date:** [December 1, 2022, 4:03pm UTC](https://community.gatling.io/t/cookie-handling-issues/7368/9 "2022-12-01T16:03:19Z")

</div>

A cookie has a domain and a path. We can default to the domain of the baseUrl if you’ve configured one, but if you don’t, you must specify it explicitly.

---

<div class="post-metadata">

**Author:** ![balaji](https://avatars.discourse-cdn.com/v4/letter/b/2bfe46/32.png) [@balaji](https://community.gatling.io/u/balaji)\
**Post date:** [December 1, 2022, 4:50pm UTC](https://community.gatling.io/t/cookie-handling-issues/7368/10 "2022-12-01T16:50:01Z")

</div>

can you please provide an example of this to specify explicitly?

.exec(getCookieValue(CookieKey(“acmToken”)))

---

<div class="post-metadata">

**Author:** ![slandelle](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/slandelle/32/4_2.png) [@slandelle](https://community.gatling.io/u/slandelle)\
**Post date:** [December 1, 2022, 4:59pm UTC](https://community.gatling.io/t/cookie-handling-issues/7368/11 "2022-12-01T16:59:03Z")

</div>

There are plenty of examples about how to use getCookie with a domain in the [documentation](https://gatling.io/docs/gatling/reference/current/http/helpers/#getting-cookie-value).

---

<div class="post-metadata">

**Author:** ![balaji](https://avatars.discourse-cdn.com/v4/letter/b/2bfe46/32.png) [@balaji](https://community.gatling.io/u/balaji)\
**Post date:** [December 2, 2022, 5:02am UTC](https://community.gatling.io/t/cookie-handling-issues/7368/12 "2022-12-02T05:02:40Z")

</div>

Thanks a lot it worked.

Have another question, seems like basic ones

How do i refer a session variable from one scala file to another?

```
.exec(session => {
  val token = session("acmToken").as[String]
  val token2 = "Bearer " + token
  session.set("acmToken", token2)
})

```

I need to use acmToken variable in another class

---

<div class="post-metadata">

**Author:** ![sbrevet](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/sbrevet/32/830_2.png) [@sbrevet](https://community.gatling.io/u/sbrevet)\
**Post date:** [December 2, 2022, 8:18am UTC](https://community.gatling.io/t/cookie-handling-issues/7368/13 "2022-12-02T08:18:07Z")

</div>

> [@balaji](#):
>
> Have another question

So please, open another thread.

Thanks!

Cheers!

---

<div class="post-metadata">

**Author:** ![sbrevet](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/sbrevet/32/830_2.png) [@sbrevet](https://community.gatling.io/u/sbrevet)\
**Post date:** [December 2, 2022, 8:18am UTC](https://community.gatling.io/t/cookie-handling-issues/7368/14 "2022-12-02T08:18:29Z")

</div>


