How to declare a constant so to make it posssible to use in http requests like .get("/myapi/${id}") ?

I just want to declare a string constant (only one constant) and then use it in http requests.
Sure, I could hard-code it. But it is not beautiful solution.

I am familiar with the documentation.

  1. Does not make sense to use feeders, because I need only one value
  2. Extracting data from responses and saving them is not an option in this case
  3. Manually set a value with session API?

I think it’s a bit cumbersome method. Is it possible just to make a variable available for all my http requests?

.get("/myapi/" + id) also does not work

“id” variable not found

I want something like

val id = 12345

.exec(http(“get my value”)
.check(status is 200)


If you only have one id value, it could make sense to store the whole url in a variable, like so:

val apiUrl = “/myapi/12345”



Still, your first example should work under the assumption you did define a variable “id” before.

However, the last example won’t work, as “${id}” in this case tells Gatling to use the Expression Language engine and looks for a key called “id” in the user’s session, which would need an “exec” block before the request to define it.


Thank you for your answer.
I’m sorry, I did not clarify my question.

apiUrl can be different, but “id” is a constant. I need a way to concatenate URL with this “id”.

For example:



Do I need an “exec” block each time when I want to use this “id” constant (or variable, does not matter)?
It is not very convenient
How can I declare a variable without using Session API?
I don’t need session which is virtual user’s state. I just need one variable to use it everywhere. Something like global variable.

You only need to use the exec block once to init the id attribute! You will be able to reuse ${id} how many times you want after, as long as it is used in an Expression Language.

Skipping the session API means using only Scala constructs over Gatling, which is what I showed in my first example.


I am new to scala. Excuse me for noob questions.

ok, let’s forget about Expression Language.

What if I will do like that:

“api_X” + myVar

How properly declare this “myVar”? And where?
I tried several options but failed :frowning:
This variable was not visible.

.get("/myapi/" + id) also does not work, “id” variable not found

100% sure it does work, as long as you define val id = “foo” BEFORE trying to actually use it to compute your url.

yes, it’s working, thanks

I am sorry but it is not working.

Should I declare this variable before each http request?

It is working only once, on the first request

error: expected session, actual validation

I do not need session! :slight_smile:

Type mismatch, expected: session.Expression[String], actual: Validation[String]