Using session attributes (or response data) within HTTP resources

Hi all,

I’m not very familiar with Gatling (or Scala) so I’m not 100% how to phrase my request, but hopefully someone will decipher what I mean and be kind enough to respond with an answer.

I used the recorder against an application that uses Angular. One of the generated HTTP requests includes several POSTs to the backend which contain parameters based on the one previous.

I want to use the JSON data from one POST response in the body of the next POST request, but I don’t know how to access it.

A simplified overview of my problem is as follows:

val scn = scenario(“UserSimulation”)
.exec(http(“request_0”).get("/").headers(headers_0).resources(

http(“request_1”).post("/service/foo").headers(headers_1).body(RawFileBody(“UserSimulation_0001_request.txt”)).check(jsonPath("$…ID").findAll.saveAs(“theIDs”)),
http(“request_2”).post("/service/bar").headers(headers_1).body(RawFileBody(“UserSimulation_0002_request.txt”)),


http(“request_xx”).get("/some/other/resource")))

// I can see the data I want with this, but I need it in request_2
.exec(session => {
val farms = session.get(“theIDs”).asOption[String]
println(farms.getOrElse(“no IDs found”))
session})

I suspect I want to replace the RawFileBody in request_2 with an EL String or an expression function, but I have no clue how to set it up.

Happy to provide more info if needed.

Cheers,
Paul

Hello,

It seems like you want to use your ids you get from your first request.

.check(jsonPath("$…ID").findAll.saveAs(“theIDs”)) => this line will create a list[String] in the session of your user with the ids found (if none are found, the check will fail and so the request)

http(“request_2”).post("/service/bar").headers(headers_1).body(RawFileBody(“UserSimulation_0002_request.txt”)) => you are using a file to generate the body of your request. To use the IDs you previously get you need to use EL. You can modify your .txt file to add EL (example: ${theIDs[0]}) and change RawFileBody to ELFileBody in your request so that your EL will be replaced with variables in your user session.

Hi Stephen,

thanks for that. I think that works. I would like to include more than one of the IDs in my JSON body (UserSimulation_0002_request.txt), say the first three members of the array. I currently have “theIDs”:["${theIDs[0]},${theIDs[1]},${theIDs[2]}],… but I can’t be sure there will be at least three IDs available for each request. Is there a say to select up to three array elements and interpolate them into the body string (or one, or two if that’s all there are)? As the body is just a text file, I’m not sure if I’m allowed to include Scala statements, or if there’s a Gatling ES way of achieving what I need.

Could I use a template object declared somewhere in my UserSimulation.scala file and access it within the RawFileBody text file?

Cheers,
Paul

Hi stephen,

If I need to loop through those id’s captured in the the list[String] and want to print them, would this ${theIDs[i]} work where i is an indexer…