still struggling with cookies...

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.


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

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:


.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”.

.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)

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: