Gatling: How to display full HTTP response body in the console or print it into a file

Using your example, just add the exec call below.

class CreateNotecard extends Simulation {  
    // . . .
    .check(bodyString.saveAs("BODY"))) 

  .exec(session => {
    val response = session("BODY").as[String]
    println(s"Response body: \n$response")
    session
  })

  // . . .
}

Printing directly from the simulation code is useful while debugging it.


There's plenty of ways to do this, from plain system.out.println() calls, whipping out some scala-code to save it to a file using your favourite java libraries, but depending on what you need the response bodies for, the easiest may be to let the logback.xml configuration do it for you.

If you've built your project from the gatling maven archetype it will already contain a logback.xml with a few commented out lines of code that contain appenders that print the entire http request/response to the console, comment in those. If you just need to see the responsebody in order to develop or debug the simulation, enabling one of the sets of these may be exactly what you need:

 <!--Uncomment for logging ALL HTTP request and responses -->
    <!--<logger name="io.gatling.http.ahc" level="TRACE" />-->
    <!--<logger name="io.gatling.http.response" level="TRACE" />-->
<!-- Uncomment for logging ONLY FAILED HTTP request and responses -->
    <!--<logger name="io.gatling.http.ahc" level="DEBUG" />-->
    <!--<logger name="io.gatling.http.response" level="DEBUG" />-->

If you want to print the response bodies to a file you could use the logback-file for this as well. I rather like using the following simple configuration, as it prints all the failing request/response logs to a file in the same catalogue as the gatling simulation results are stored, useful to see in more detail what kind of errors you get in your simulations in a dedicated file.

For Gatling 2.3:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%-5level] %logger{15} - %msg%n%rEx</pattern>
        </encoder>
    <immediateFlush>false</immediateFlush>
    </appender>

    <appender name="ERROR" class="ch.qos.logback.core.FileAppender">
        <file>target/gatling/simulation-errors.log</file>
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%-5level] %logger{15} - %msg%n%rEx</pattern>
        </encoder>
        <immediateFlush>false</immediateFlush>
        <param name="Append" value="false" />
    </appender>

    <logger name="io.gatling.http.ahc" level="DEBUG" additivity="false">
        <appender-ref ref="ERROR"/>
    </logger>
    <logger name="io.gatling.http.response" level="DEBUG" additivity="false">
        <appender-ref ref="ERROR"/>
    </logger>

    <root level="WARN">
        <appender-ref ref="CONSOLE" />
    </root>

</configuration>

For Gatling 3.0, the two loggers above must be replaced with the following appender:

<logger name="io.gatling.http.engine.response" level="DEBUG" additivity="false">
    <appender-ref ref="ERROR"/>
</logger>

This is the solution according to gatling-sbt-demo-documentation for gatling 3.2.0

<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
        <pattern>%d{HH:mm:ss.SSS} [%-5level] %logger{15} - %msg%n%rEx</pattern>
    </encoder>
    <immediateFlush>false</immediateFlush>
</appender>

<!-- uncomment and set to DEBUG to log all failing HTTP requests -->
<!-- uncomment and set to TRACE to log all HTTP requests -->
<!--<logger name="io.gatling.http.engine.response" level="TRACE" />-->

<root level="WARN">
    <appender-ref ref="CONSOLE" />
</root>

Just uncomment <!--<logger name="io.gatling.http.engine.response" level="TRACE" />--> as you did in the past.