Gatling request based on previous request

I’m new to Scala.

I’m using Gatling for stress testing.

I’m able to make a Gatling test that makes a request to a WS, I save the JSON response in the session variable. The response is a JSON array that contains several links to images that are provided by my backend.

Specifically, The first request retrieves points in a map, each point has an image assigned, each image must be fetched by accessing the link provided by the response of the first WS.

I have following code:

class BasicSimulation extends Simulation
{

object Points
{
val jsonFileFeeder = jsonFile(“input.json”).circular
val points=exec(http(“r1”).get("/"))
.feed(jsonFileFeeder)
.exec(http(“r2”)
.post("/ws/getPoints")
.check(bodyString.saveAs(“points”))
)
}

object Images
{
val images=exec(session=>{
val respMap = session(“points”).as[String]
val mapper = new ObjectMapper()
val rA = mapper.readTree(respMap)
for( a ← 0 until (rA.size()-1))
{
val lnk=rA.get(a).get(“image”).toString()
exec(http(“r3”).get(lnk))
}
session
}
)
}

val httpConf = http
.baseURL(“http://localhost:8000/”)
.userAgentHeader(“Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:16.0) Gecko/20100101 Firefox/16.0”)

val scn = scenario(“Test1”).exec(Points.points,Images.images)
setUp(scn.inject(atOnceUsers(2)).protocols(httpConf))
}

A sample of the JSON response of the first WS:

[
{
“bid”: 1375,
“image”: “http://localhost:8000/ws/image/1375”,
“position”: [2.326609,48.872678],
},
{
“bid”: 1375,
“image”: “http://localhost:8000/ws/image/1375”,
“position”: [2.352725,48.87323],
}
]

The first request works fine, I don’t parse the response with jsonPath since I get always the error:

could not extract : string matching regex [$_\p{L}][$_\-\d\p{L}]*' expected but [’ found

Though I have veryfied my jsonpath expression with

https://jsonpath.curiousconcept.com/

With

import io.gatling.core.json.Jackson

I’m able to parse the response, the problem is that when trying to make
the second request exec(http(“r3”).get(lnk)), the request isn’t made, since
I’m logging on backend size the requests that are made, when making the first request, the backend logs the request, when making the second request, the request is not logged on backend side.

If I put the request directly depending on the scenario:

scenario(“Test1”).exec(http(“r”).get(“http://localhost:8000/ws/image/1375”))

The request is made.

I want to make a request to WS, parse the response, iterate over the JSON elements of the response and for each element make a second call to other webservice.

Thank you for your help.

I was in the process on answering on SOF…

Could you please provide your JsonPath tentatives?

The JsonPath spec is sometimes vague, and there’s isn’t a single implementation used by those only evaluators that’s not buggy.
I’m not saying that there isn’t an issue in Gatling one’s, but that those online evaluators can’t be trusted 100%. If you want to be sure, read the spec.

Based on this JSON example

[

{
“bid”: 1375,
“image”: “http://localhost:8000/ws/image/1375”,
“position”: [
2.326609,
48.872678
]
},

{
“bid”: 1375,
“image”: “http://localhost:8000/ws/image/1375”,
“position”: [
2.352725,
48.87323
]
}

]

And using this online expression tester:
https://jsonpath.curiousconcept.com/

I have tried:
$[]
$

$…*

As it is stated on https://jsonpath.curiousconcept.com/
the implementation JsonPath used on the site is 0.3.1

Nevertheless, my main interest is to make the second request to the WS, based on the response of the first WS. I’m able to parse the response with the Jackson package.

Thank you very much.

Hello, I have finally parsed the response with :

jsonPath("$[*].image").ofType[String].findAll.saveAs(“images”)

My problem is now that I cannot make a http request for each link parsed by jsonPath.

val render=foreach("${images}",“image”){
exec{
session=>
exec(http(“Myreques”).get("${image}"))
session
}
}

The request is not done, since on backend side I don’t get any logs.

Thank you.