How to store JSON response (or part of it) in temporary variable?

Hi,

I wrote a very basic load test in Gatling 2.0 RC2 which hits a couple different REST endpoints (see below). One of them returns JSON. I would like to extract data from the response and use it in a subsequent REST call. This seems to be possible with a combination of jsonPath/asJSON/check/saveAs. But I couldn’t find a working example yet that uses all of those in combination.

POSTing to /merchant returns something like

{
“Id”: 5039424849051648,
“Description”: “a”,
“Modulo”: 1
}

I want to extract the “Id” field from the response and use it in a GET call to /merchant?m=${id}.

val scn = scenario(“Gatling test”)

.repeat(10) {
exec(http(“create merchant”).put("/merchant").body(StringBody("{“Description”:“test”,“Modulo”:5}")).asJSON.check(bodyString.transform(string => string).saveAs(“merchantId”)))

.exec(http(“get merchant”).get("/merchant?m=${merchantId}"))
// …

}

// …

My current expression puts the entire response of POST /merchant into $merchantId. How can I change the exec() call to extract the Id field?

Thanks,
Ingo

This did the trick:

.exec(http(“create merchant”).put("/merchant").body(StringBody("{“Description”:“test”,“Modulo”:5}")).check(jsonPath("$.Id").saveAs(“merchantId”)))

It is partially described in the docs on http://gatling.io/docs/2.0.0-RC3/http/http_check.html#checks

Thanks,
Ingo