How to make sure a request is run only if a particular key value exists

Hi all,

I have 2 requests say A1 and A2
In req A1
I Have something like

val *A1*: ChainBuilder = exec(http("")
  .post("endpoint")
  .headers(headers)
  .body(StringBody("""{"key":id}"""))
  .check(jsonPath("""$..Id""").saveAs("Id"))
  .check(status.is(200)))
  .exec(session => {
   println()
    session
  })

Now I want that req A2 is build only if key Id has some value

I am trying

val *A2*: ChainBuilder = {doIf(!"${Id}".isEmpty) {
exec(http("")
  .post("endpoint")
  .headers(headers)
  .body(StringBody("""{"key":id}"""))
  .check(jsonPath("""$..Id""").saveAs("Id"))
  .check(status.is(200)))
  .exec(session => {
   println()
    session
  })'
}

This does not work as intended and even if the session variable Id is empty , it works.
Now I think it might be because the if condition is checking the value as string and not the session variable 
I am definitely missing something
Could someone suggest a way to achieve the requirement
Thanks

It’s all in the documentation :slight_smile:

https://gatling.io/docs/current/session/expression_el/

Oh Thanks a lot , I got it

doIf("${Id.exists()}")

It worked using this.

:+1:

Hi,
One more issue
now , ("${Id.exists()}") this returns a boolean value .
So if we want to club this with supposed other lets say ("${name.exists()}") in the same doIf condition, how to achieve that.
Because && operator is not working for this

You can’t use Gatling EL for complex logic. You have to code this with the Session API.