Search code examples
scalahttpgatling

Retrieve cookie value with Gatling


I'm kinda new to Gatling and I'd like to get the value from a cookie. I tried many ways to do so but I might misunderstand something.

At first I'm doing a post request to my auth API which create the cookie I want.

Then I've tried :

   .exec {
      session => println(session)
      println(session.attributes)

      // return a Some object whose value is of type CookieJar (with apparently private access)
      println(session.attributes.get("gatling.http.cookies"))

      /*
      // Doesn't compile due to CookieJar being private
      val value: CookieJar = session.attributes.get("gatling.http.cookies") match {
        case None => None
        case Some(cj: CookieJar) => cj
      }
      print(value)
      */

      // return a GetCookieBuilder which doesn't seem really useful
      println(getCookieValue(CookieKey("COOKIE_NAME")))

      session
    }

Do you have any idea about it ?


Solution

  • getCookieValue is a DSL component, not a method you can call in your own functions.

    It's used as a scenario step to extract a cookie value from the internal CookieJar and copy it in the Session as a dedicated attribute.

    exec(getCookieValue(CookieKey("COOKIE_NAME")))
    .exec { session =>
      println(session("COOKIE_NAME").as[String])
      session
    }