RESTful API with JSON response: how to copy data from JSON response into gatling Session map

I am trying to copy data from the json response into the Session so I can use them for subsequent calls.
The best I got is the below code, but the “${response.get(“skey”)}” does not work and I am out of ideas how I could do it.

Am I on the wrong path here? Thanks!

def parseLoginResponse(body: String) : Map[String,Any] = {
val json:Option[Any] = JSON.parseFull(body)
val response:Map[String,Any] = json.get.asInstanceOf[Map[String, Any]]

val user_id:Double = response.get(“user_id”).get.asInstanceOf[Double]
val skey:String = response.get(“skey”).get.asInstanceOf[String]
val success:Boolean = response.get(“success”).get.asInstanceOf[Boolean]

Map(“user_id” → user_id.toInt, “skey” → skey, “success” → success)
}

val scn = scenario(“scenario_1”)
.feed(user_info_generator)

.exec(
http(“step_1”)
.post("/login/")
.param(“uid”, “${username}”)
.headers(headers_3)
.check(status.is(200),
bodyString.transform( body => {
parseLoginResponse(body)
}
).saveAs(“response”))
)
.exec(

http(“step_2”)
.post("/do/something/")
.param(“user_id”, “${response.get(“user_id”)}”)
.param(“skey”, “${response.get(“skey”)}”)
.headers(headers_3)
.check(status.is(200))
)

Hi,

You can use the jsonPath check to save the value contain in the response in the session (https://github.com/excilys/gatling/wiki/Checks#wiki-json). Therefore you can try something like that.

val scn = scenario(“scenario_1”)
.feed(user_info_generator)

.exec(
http(“step_1”)
.post("/login/")
.param(“uid”, “${username}”)
.headers(headers_3)
.check(status.is(200),
jsonPath("$.user_id").exists.saveAs(“userId”),
jsonPath("$.skey").exists.saveAs(“skey”)
)
.exec(

http(“step_2”)
.post("/do/something/")
.param(“user_id”, “${userId}”)
.param(“skey”, “${skey}”)
.headers(headers_3)
.check(status.is(200))
)

Hope it helps,

Sébastien

Hey,

thanks so much for your help.

That one works great.
The “$.user_id” did the trick. We tried “/user_id” and “//user_id” before.

Cheers,
Mike

Before 1.5, we had our own custom JsonPath stuff, different from Goessner’s one and much alike XPath. We changed that and shipped Jayway’s Goessner’s implementation.
We’re also building our own implementation and will ship it in a next version.

Cheers,

Stéphane