To do that you can use method .transform()
on bodyString
. It takes function parameter of type String => T
or (String, Session) => T
and applies it on response body, so in Your case it would look like:
.check(
bodyString.transform(
(body: String, session: Session) => {
if(body.contains("success")){
File("/path/to/file")
.createFile()
.appendAll(session("attributeName").as[String])
}
}
)
)
-- Edited --
So to save part of request body to session what you have to do is to generate that part separately, save into session attribute and use when creating body. For example lets assume that you want to send JSON containing 2 fields: some constant value + current timestamp, and then save timestamp to file if response contains "success":
val exampleScenario = scenario("Example")
.exec(session => {
session.set("timestamp", System.currentTimeMillis)
})
.exec(http("Send data")
.post("http://example.com")
.body(StringBody("""{"constant":123, "timestamp": ${timestamp}}"""))
.asJSON
.check(
bodyString.transform(
(body: String, session: Session) => {
if(body.contains("success")){
File("/path/to/file")
.createFile()
.appendAll(session("timestamp").as[String])
}
}
)
)
)