printing "bodyString.saveAs" with a custom function

@Stéphane Landelle

In the following example, I have two api calls covered. I wanted to print the response of each api call in the logs but don’t want to duplicate writing the same code. I understand enabling <!-- –prints everything in the logs. But that prints huge data set. we are interested in just the body (i.e. api response)

val paj_request = exec(http("/platform/spi/auth/visitor/login")

.post("/platform/spi/auth/visitor/login")
.formParam(“username”,“xxxx”)
.formParam(“password”,“xxxxx”)
.headers(requestHeaders)
.check(bodyString.saveAs(“apiresponse”))
)

printapiresponse("/platform/spi/auth/visitor/login")

.feed(custRandUserFeeder)
.exec(http("/platform/spi/job/v2/post")
.post("/platform/spi/job/v2/post")
.body(ElFileBody(“data/jobDynamic.json”)).asJson
.headers(requestHeaders_1)
.header(“X-AuthToken”, “${authToken}”)
.check(bodyString.saveAs(“apiresponse”))
)

printapiresponse("/platform/spi/job/v2/post")

def printapiresponse(args: String): Unit = {
exec { session =>
println(args + “>>>>>>api-response is” + session(“apiresponse”).as[String] + “<<<<<<”)
session
}
}

If my understanding is correct, printing session needs to be attached to the parent exec which stores bodyString. but that would require writing duplicate code for every api request.

right now in the above example nothing gets printed. Wondering if there is some way I can achieve above. could someone help here?

Thanks,
Vijay