If session exist attribute set the #{attribute} to real value, and if its null set #{attribute} to null, how can I do?

Our API sequence is A->B

1step: extract API A’s discountPremuim and set it to session

.check(jsonPath("$[0].discountPremium").optional.saveAs("discountPremium"))

2step: API B’s json feeder use this discountPremuim

{        
	...
	"discountPremium": #{discountPremium},
	...
}

But API A may not respond the discountPremuim if goods don’t have the discount.

I try use

      .exec(_.set("discountPremium",null.asInstanceOf[Any]))

to set it to null or directly set it to null, but Failed to build request: Attribute discountPremium’s value is null
here’s what I want

        "discountPremium": #{discountPremium.exist()?discountPremium:null},

I can not find the Gatling EL’s ternary operator on the gatling.io
how can I do? many thanks

2 things:

  • Gatling EL is fairly simple and doesn’t support conditional logic
  • In this situation, you say you want "discountPremium": null ie the field to be present but with a null value. But some other users might want the discountPremium field to be completely absent. So there’s no one-size-fit-all solution for this problem.

What I recommend is to not use Gatling Expression Language for complex templating but use Pebble instead.

1 Like

Many thanks, slandelle, I will try it. :green_heart: