I got following Jason body in my request. I have captured value for event_token in eventToken parameter and substituting this as per below in Jason body
so I am trying and substituting this in son body as follows but it's failing and don't really understand what's wrong in this syntax for substituting parameter value. Please suggest ?
**.body(StringBody(session =>**
** s"""**
** {**
** | "event": {**
** | "id": “CREATE”,**
** | "summary": "4th Page Check your answers - Event summary 9999999",**
** | "description": "4th Page Check your answers - Event description 9999999"**
** | },"event_token": """" + "${eventToken}" + """",**
** | "ignore_warning": false**
** |}**
** """.stripMargin)).asJSON**
**however if I am trying this hardcoded value then it's working OK because this token is valid for 1 hour atleast.**
What I really don’t understand is why you rely on Scala’s String interpolation, instead of storing the variables you need in Gatling’s Session. I’ve coded an example (maybe it doesn’t work, but you can see what I mean).
I find easier to store the variables in a previous step (you can always remove them in next steps).
class KapilSimulation extends Simulation {
private val rng: Random = new Random()
private def createToken(): String = rng.alphanumeric.take(32).mkString
private def createRandomInt(): Int = rng.nextInt(999999999)
setUp(
scenario("My Scenario")
.exec(
_.setAll(
("eventToken", createToken()),
("eventRandomInt", createRandomInt())
)
).exec(
http("My HTTP request")
.post("/random")
.body(StringBody("""{"token": "${eventToken}", "number": ${eventRandomInt}}""")).asJSON
.check(status is 200)
).inject(atOnceUsers(5))
).protocols(
http.baseURL(gatewayUrl)
.acceptHeader("application/json")
.contentTypeHeader("application/json")
.acceptEncodingHeader("gzip, deflate")
.userAgentHeader("Gatling")
.warmUp(s"$gatewayUrl/health")
.shareConnections
).maxDuration(duration)
}