Save a part of the URL with Gatling

I really like Gatling. Mainly because the scenario are coded in Scala. Don't get me wrong. I don't like Scala. It's way too complicated for its own good. But I do like to have scenarios written in a real programming language and a nice DSL.

So today, my goal was to retrieve a magic token that was appearing in my response URL to be able to reuse it for other URL.

Here's how to do it:

exec(http("home")
  .get("/")
  .headers(headers)
  .check(currentLocation.transform(s => {
    val pattern = """.*/beforemytoken/(.*)/.*""".r
    val pattern(temp) = s
    temp
  }).saveAs("magictoken"))
)
.exec(http("query")
  .get("/${magictoken}/stuff")
  .headers(headers)
)