Send different HTTP request body every time

I have a file with a Json request bodies in a file. I’d like to benchmark a web sending a constant number of requests per second, using the constantUsersPerSec(…).

However, every time a request is sent, I would like it to be taken from a list of requests (randomly, or in a loop, I don’t care).

I cannot figure out how to do it with Gatling. Using the following code the request is randomly selected once and the same request is sent over and over again. Which is not what I want

My code:

class GcStressManyRequests extends Simulation{ 
  ...
  ...
    val httpProtocol: HttpProtocolBuilder = http
    .baseURL(baseUrl)
    .contentTypeHeader(contentType)  

    var requests = Source.fromResource("bodies/requests.txt").getLines().toIndexedSeq
    val random = new Random(System.currentTimeMillis())

    val scn: ScenarioBuilder = scenario("AE prod requests")
    .exec (
         http("bid_request")
         .post(endpoint)
         .body(StringBody(requests(random.nextInt(requests.length))))
         .asJSON
         .check(status.is(200))) 

         setUp(
         scn.inject(
         constantUsersPerSec(400) during (1 minutes),
         ).protocols(httpProtocol))

 }

Hi Michael.

try the code below:

val scn: ScenarioBuilder = scenario(“AE prod requests”)
.exec(session => {
val session2 = session.set(“therequest”, requests(random.nextInt(requests.length)))
session2
})
.exec (
http(“bid_request”)
.post(endpoint)
.body(StringBody("${therequest}"))
.asJSON
.check(status.is(200)))

Yes!
That is exactly what I was looking for. I wish that example was in the official documentation.
Thank you so much!!

You can try

.body(StringBody(session => requests(random.nextInt(requests.length)))).asJSON

Reference
https://groups.google.com/forum/#!msg/gatling/Anl1Kuvbk0A/eWYlcbdNPIMJ

that is neat!!