Save set-cookie value

969 views
Skip to first unread message

PM

unread,
Dec 5, 2017, 3:30:07 AM12/5/17
to Gatling User Group
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?

Stephen

unread,
Dec 5, 2017, 3:49:53 PM12/5/17
to Gatling User Group
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

Adam Artajew

unread,
Dec 6, 2017, 3:42:06 PM12/6/17
to Gatling User Group
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!

gatling...@gmail.com

unread,
Dec 6, 2017, 7:21:33 PM12/6/17
to Gatling User Group
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

Adam Artajew

unread,
Dec 7, 2017, 4:17:53 AM12/7/17
to Gatling User Group
Hi,

if you just want to print the cookie, try this:

exec(session => {
  println(TakeCookieFromJar("cookieName", session))
  session
})


Cheers!

gatling...@gmail.com

unread,
Dec 8, 2017, 3:04:54 PM12/8/17
to Gatling User Group
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))})

------------------------------------

Error:

13:59:02.642 [main][ERROR][ZincCompiler.scala:140] i.g.c.ZincCompiler$ - exec(session => {session.set("my_cookie_value", TakeCookieFromJar("cookieName", session))})

13:59:02.643 [main][ERROR][ZincCompiler.scala:140] i.g.c.ZincCompiler$ - ^


Adam Artajew

unread,
Dec 8, 2017, 3:17:21 PM12/8/17
to Gatling User Group
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 :)
Reply all
Reply to author
Forward
0 new messages