# still struggling with cookies...

**URL:** <https://community.gatling.io/t/still-struggling-with-cookies/6372>\
**Category:** Gatling (Open-Source)\
**Created:** [January 1, 2022, 2:06pm UTC](https://community.gatling.io/t/still-struggling-with-cookies/6372 "2022-01-01T14:06:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stef\_Joosten](https://avatars.discourse-cdn.com/v4/letter/s/a88e4f/32.png) [@Stef\_Joosten](https://community.gatling.io/u/Stef_Joosten)\
**Post date:** [January 1, 2022, 2:06pm UTC](https://community.gatling.io/t/still-struggling-with-cookies/6372/1 "2022-01-01T14:06:45Z")

</div>

I want to use a cookie from my session in my scenario. For this purpose I get the cookie (using `getCookieValue`) and use it further down the line as `session(PHPSESSID)`.

Here is a fragment of my scenario.

```auto

.pause(2)
.exec(getCookieValue(CookieKey("PHPSESSID")))
.exec { session =>
http("Fill Userid/Password")
.patch("/api/v1/resource/SESSION/1")
// .headers(headers_sessID)
.body(StringBody(""" [{ "op":"replace",
"path":"/Login/"""+session("PHPSESSID")+"""/Login/"""+session("PHPSESSID")+"""/Userid",
"value":"peter"
},
{ "op":"replace",
"path":"/Login/"""+session("PHPSESSID")`
+"""/Login/"""+session("PHPSESSID")+"""/Password",
"value":"stupidpassword"
},
{ "op":"replace",
"path":"/Login/"""+session("PHPSESSID")+"""/Login/"""+session("PHPSESSID")+"""/Login/property",
"value":true
}] """)).asJson
session}
.pause(2)

```

My problem is that this does not work. the step “Fill Userid/Password” is not being executed.  
Maybe that is because it is a function?  
However, If I do not define the exec step as a function, it cannot find `session("PHPSESSID")` because `session` is not defined.

I’d get grateful for a good explanation that drives me toward a solution.

---

<div class="post-metadata">

**Author:** ![Stef\_Joosten](https://avatars.discourse-cdn.com/v4/letter/s/a88e4f/32.png) [@Stef\_Joosten](https://community.gatling.io/u/Stef_Joosten)\
**Post date:** [January 2, 2022, 12:30pm UTC](https://community.gatling.io/t/still-struggling-with-cookies/6372/2 "2022-01-02T12:30:51Z")

</div>

I found an answer, but it does not use the solution suggested by the Gatling documentation, which is `exec(getCookieValue(CookieKey("PHPSESSID")))`.  
So it is not really satisfactory, but it works… Here is the code fragment that works:

```auto

.pause(2)
.exec(
http("Display the login screen")
.get("/api/v1/resource/SESSION/1/Login")
.check(jsonPath("$._id_").find.saveAs("sessId"))
)
.pause(2)
.exec (
http("Fill Userid/Password")
.patch("/api/v1/resource/SESSION/1")
.body(StringBody(""" [{ "op":"replace",
"path":"/Login/${sessId}/Login/${sessId}/Userid",
"value":"peter"
},
{ "op":"replace",
"path":"/Login/${sessId}/Login/${sessId}/Password",
"value":"stupidpassword"
},
{ "op":"replace",
"path":"/Login/${sessId}/Login/${sessId}/Login/property",
"value":true
}] """)).asJson
)
.pause(2)

```

So here I did not get the session id from the cookie PHPSESSID, but I got it from filtering it out from a response message in the step “Display the login screen”.

---

<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:** [January 2, 2022, 9:25pm UTC](https://community.gatling.io/t/still-struggling-with-cookies/6372/3 "2022-01-02T21:25:45Z")

</div>

.pause(2)  
.exec(getCookieValue(CookieKey(“PHPSESSID”)))  
.exec { session =\>  
http(“Fill Userid/Password”)  
.patch("/api/v1/resource/SESSION/1")  
// .headers(headers\_sessID)  
.body(StringBody(""" [{ “op”:“replace”,  
“path”:"/Login/#{PHPSESSID}/Login/#{PHPSESSID}/Userid",  
“value”:“peter”  
},  
{ “op”:“replace”,  
“path”:"/Login/#{PHPSESSID}/Login/#{PHPSESSID}/Password",  
“value”:“stupidpassword”  
},  
{ “op”:“replace”,  
“path”:"/Login/#{PHPSESSID}/Login/#{PHPSESSID}/Login/property",  
“value”:true  
}] “”")).asJson  
session}  
.pause(2)

---

<div class="post-metadata">

**Author:** ![Stef\_Joosten](https://avatars.discourse-cdn.com/v4/letter/s/a88e4f/32.png) [@Stef\_Joosten](https://community.gatling.io/u/Stef_Joosten)\
**Post date:** [January 6, 2022, 11:37am UTC](https://community.gatling.io/t/still-struggling-with-cookies/6372/4 "2022-01-06T11:37:41Z")

</div>

Thank you, Stéphane. I tried your solution because it looks like “the right way to do things”. However, I got back the following results:

```auto

```
