In Gatling, displaying the full HTTP response body in the console or saving it to a file requires handling the response appropriately within your simulation scripts. Here's how you can achieve this:
To print the full HTTP response body to the console during or after a simulation run, you can use Gatling's logging capabilities. By default, Gatling logs the request and response summary, but for detailed logging, you can customize it.
Custom Logging Configuration: Gatling uses SLF4J for logging, and you can configure it to log HTTP response bodies explicitly.
logback.xml or logback-test.xml file in your Gatling project's src/test/resources directory to customize logging.Here's an example of configuring Logback to log HTTP request and response bodies:
<configuration> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%date{HH:mm:ss.SSS} [%-5level] [%logger{36}] - %msg%n</pattern> </encoder> </appender> <logger name="io.gatling.http.ahc.HttpHandler" level="DEBUG" /> <root level="DEBUG"> <appender-ref ref="STDOUT" /> </root> </configuration> <logger name="io.gatling.http.ahc.HttpHandler" level="DEBUG" /> specifies that HTTP handler logs should be set to DEBUG level, which includes HTTP request and response bodies.Simulation Code: Ensure your Gatling simulation script captures the response body and logs it appropriately. Here's an example snippet:
import io.gatling.core.Predef._ import io.gatling.http.Predef._ import scala.concurrent.duration._ class MySimulation extends Simulation { val httpConf = http.baseUrl("http://example.com") val scn = scenario("MyScenario") .exec(http("request") .get("/api/something") .check(status.is(200)) .check(bodyString.saveAs("responseBody")) // Save response body to session ) .exec(session => { println(session("responseBody").as[String]) // Print response body session }) setUp( scn.inject(atOnceUsers(1)) ).protocols(httpConf) } .check(bodyString.saveAs("responseBody")) saves the response body to the Gatling session.println(session("responseBody").as[String]) prints the response body to the console.If you want to save the response body to a file instead of printing it to the console:
Modify the Gatling simulation script to write the response body to a file using standard Scala file I/O operations (java.nio.file).
Example of saving the response body to a file:
import java.nio.file.{Paths, Files} import java.nio.charset.StandardCharsets val scn = scenario("MyScenario") .exec(http("request") .get("/api/something") .check(status.is(200)) .check(bodyString.saveAs("responseBody")) // Save response body to session ) .exec(session => { val responseBody = session("responseBody").as[String] Files.write(Paths.get("response.txt"), responseBody.getBytes(StandardCharsets.UTF_8)) session }) Files.write(Paths.get("response.txt"), responseBody.getBytes(StandardCharsets.UTF_8)) writes the response body to a file named response.txt in the current directory.Gatling print full HTTP response body to console
val scn = scenario("ScenarioName") .exec(http("RequestName") .get("/api/endpoint") .check(bodyString.saveAs("responseBody"))) .exec { session => println(session("responseBody").as[String]) session } Gatling save HTTP response body to a file
val scn = scenario("ScenarioName") .exec(http("RequestName") .get("/api/endpoint") .check(bodyBytes.saveAs("responseBody"))) .exec { session => val responseBody = session("responseBody").as[Array[Byte]] new FileOutputStream("response.txt").write(responseBody) session } Gatling display full HTTP response body in logs
val scn = scenario("ScenarioName") .exec(http("RequestName") .get("/api/endpoint") .check(bodyString.transform(s => { println(s) s }))) Gatling log HTTP response body with headers
val scn = scenario("ScenarioName") .exec(http("RequestName") .get("/api/endpoint") .check(bodyString.saveAs("responseBody"))) .exec { session => val responseBody = session("responseBody").as[String] val headers = session("response").as[Response].headers println(s"Response Headers: $headers") println(s"Response Body: $responseBody") session } Gatling save full HTTP response body as variable
val scn = scenario("ScenarioName") .exec(http("RequestName") .get("/api/endpoint") .check(bodyString.saveAs("responseBody"))) .exec { session => val responseBody = session("responseBody").as[String] println(s"Response Body: $responseBody") session } Gatling extract and print HTTP response body
val scn = scenario("ScenarioName") .exec(http("RequestName") .get("/api/endpoint") .check(bodyString.transform(s => { println(s"Response Body: $s") s }))) Gatling print HTTP response body with status
val scn = scenario("ScenarioName") .exec(http("RequestName") .get("/api/endpoint") .check(status.is(200), bodyString.saveAs("responseBody"))) .exec { session => val responseBody = session("responseBody").as[String] println(s"Response Status: ${session("status").as[Int]}") println(s"Response Body: $responseBody") session } Gatling save HTTP response body to variable
val scn = scenario("ScenarioName") .exec(http("RequestName") .get("/api/endpoint") .check(bodyString.saveAs("responseBody"))) .exec { session => val responseBody = session("responseBody").as[String] println(s"Response Body: $responseBody") session } Gatling display complete HTTP response in console
val scn = scenario("ScenarioName") .exec(http("RequestName") .get("/api/endpoint") .check(bodyString.transform(s => { println(session("response").as[String]) s }))) Gatling log HTTP response body to file
val scn = scenario("ScenarioName") .exec(http("RequestName") .get("/api/endpoint") .check(bodyBytes.saveAs("responseBody"))) .exec { session => val responseBody = session("responseBody").as[Array[Byte]] val file = new java.io.File("response.txt") val fos = new java.io.FileOutputStream(file) fos.write(responseBody) fos.close() session } uiscrollviewdelegate automake file-get-contents bootstrap-datetimepicker hibernate-entitymanager swagger-codegen throwable uistackview spinnaker executable-jar