PM1
December 5, 2017, 8:30am
1
Hello,
I have the following response:
headers=
content-type: application/json; charset=utf-8
set-cookie: test1=eyJhbGciOiJIUzI1NiI ; Max-Age=31536000; Path=/
set-cookie: test2=7df22c2c-b8b7-4d1b-8aee-a65e8d73dc99; Max-Age=31536000; Path=/
cache-control: no-cache
Connection: keep-alive
body=
{…}
I need to store the value of the set-cookie: test2 ----> 7df25c2c-b257-451b-86e-a55e5d73dc99
Can someone help me with this?
Hello,
Since you are trying to get the value of a cookie, I suggest you to have a look a this post: https://groups.google.com/forum/#!topic/gatling/OWVVoWMsFd0
Stephen
Hi,
this code works for me:
`
package loadtest
import io.gatling.core.session._
import io.gatling.http.cookie.CookieSupport
object TakeCookieFromJar {
val baseUrl = “http://example.com ”
def apply(name: String, session: Session): String = {
CookieSupport.getStoredCookies(session, baseUrl).find(_.getName == name) match {
case Some(cookie) => cookie.getValue
case _ => throw new Exception(s""“Cookie “$name” not found!”"")
}
}
}
// usage
exec(session => session.set(“my_cookie_value”, TakeCookieFromJar(“cookieName”, session)))
`
Enjoy!
Thank you for this Adam. How would you print the cookie value to the console? Where would the println() statement go in this case and what would the cookie reference be? Thank you
Hi,
if you just want to print the cookie, try this:
`
exec(session => {
println(TakeCookieFromJar(“cookieName”, session))
session
})
`
Cheers!
Adam, thanks again! I am a newbie at this and could use a bit more consult. I cannot get your code to compile - this below and the one prior. Any help you can provide is much appreciated. This is what I put together using your work.
package testPackage
import scala.concurrent.duration._
import io.gatling.core.Predef._
import io.gatling.http.Predef._
import io.gatling.jdbc.Predef._
import io.gatling.http.cookie._
import io.gatling.http.cookie.CookieSupport
object TakeCookieFromJar {
val baseUrl = “http://example.com ”
def apply(name: String, session: Session): String = {
CookieSupport.getStoredCookies(session, baseUrl).find(_.getName == name) match {
case Some(cookie) => cookie.getValue
case _ => throw new Exception(s""“Cookie “$name” not found!”"")
}
}
}
// usage
exec(session => {session.set(“my_cookie_value”, TakeCookieFromJar(“cookieName”, session))})
Hi,
you need to remove the usage example (the last 2 lines), it was just to show you how to use in the scenario workflow