Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/gatling/gatling.io-doc/llms.txt

Use this file to discover all available pages before exploring further.

Gatling’s Expression Language (EL) covers most templating needs, but sometimes you need to compute a parameter that goes beyond simple attribute lookup—parsing a value, combining multiple attributes, performing arithmetic, or applying conditional logic. For those cases, Gatling SDK methods that accept a String or value argument also accept a function that receives the virtual user’s current Session and returns the desired value. The function is evaluated freshly for each virtual user every time the action is executed, making it fully dynamic.
Functions run on Gatling’s shared threads. Never perform blocking I/O (network calls, file reads, database queries) inside a function—doing so will stall all virtual users sharing that thread and severely degrade test accuracy.
Gatling SDK components are definitions, not imperative instructions. They have no effect when called inside functions. For example, calling exec(http(...)) inside a lambda does nothing—SDK calls must be chained at the scenario-definition level and passed to setUp.

Function Signatures

The exact signature of a function parameter depends on which SDK language you use.
In Java, functions take a Session and return a value of the expected type T:
Session -> T
import io.gatling.javaapi.core.Session;

http("dynamic request")
  .get(session -> "/users/" + session.getString("userId"))
  .queryParam("locale", session -> session.getString("language").toLowerCase())
  .body(StringBody(session ->
    "{\"age\": " + session.getInt("age") + ", " +
    "\"name\": \"" + session.getString("name") + "\"}"
  ));

Session API

The Session object provides typed accessors for reading attributes that were previously stored by feeders, checks, or exec blocks.
session.getString("key")         // returns String or null
session.getInt("key")            // returns int (auto-unboxed)
session.getLong("key")           // returns long
session.getDouble("key")         // returns double
session.getBoolean("key")        // returns boolean
session.getList("key")           // returns List<Object>
session.getMap("key")            // returns Map<String, Object>
session.contains("key")          // returns boolean

Practical Examples

Constructing a URL from Session Data

Java
http("get user orders")
  .get(session ->
    "/users/" + session.getString("userId") + "/orders?page=" + session.getInt("page"))

Combining Multiple Attributes in a Body

Java
http("place order")
  .post("/orders")
  .body(StringBody(session ->
    String.format(
      "{\"product\": \"%s\", \"quantity\": %d, \"price\": %.2f}",
      session.getString("productId"),
      session.getInt("qty"),
      session.getDouble("unitPrice") * session.getInt("qty")
    )
  ));

Conditional Logic

Java
http("request with optional header")
  .get("/resource")
  .header("X-Admin", session ->
    "admin".equals(session.getString("role")) ? "true" : "false"
  )

Scala for-comprehension with Validation

Scala
http("safe combined param")
  .get(session =>
    for {
      userId  <- session("userId").validate[String]
      groupId <- session("groupId").validate[String]
    } yield s"/groups/$groupId/users/$userId"
  )

When to Use Functions vs. EL

SituationBest Approach
Simple attribute substitutionEL expression: "#{username}"
Nested or indexed attribute accessEL: "#{items(0)}", "#{user.email}"
Arithmetic or string operationsFunction
Combining multiple attributesFunction
Conditional/branching logicFunction
Type coercion (e.g., String → Int)Function
In Scala, you can also write a helper method that returns Expression[T] and compose it using map/flatMap on Validation. See the Validation reference for details.

Build docs developers (and LLMs) love