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

3

23

I'm new to Gatling. I could not find a simple complete example of how to see the full HTTP response body.

This is my simple example

class CreateNotecard extends Simulation {  
    val baseURL = "https://portal.apps.stg.bluescape.com" 
    val httpConf = http 
        .baseURL(baseURL) 
        .userAgentHeader("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36") 

    val scn = scenario("Create a notecard")  
        .exec(http("Get authenticity token") 
        .get("/users/sign_in") 
        .check(bodyString.saveAs("BODY"))) 

    setUp( 
        scn.inject(atOnceUsers(1))  
    ).protocols(httpConf)  
}

How can I print the bodyString into a file or on the console?

Thanks in advance

Jacquline answered 27/10, 2017 at 0:7 Comment(0)
A
16

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>
Advertise answered 27/10, 2017 at 8:14 Comment(4)
Thank you, Kim. That was helpful. I found the following simple programming solution by adding extraInfoExtractor val httpProtocol = http .baseURL(baseURL) .userAgentHeader("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36") .extraInfoExtractor(ExtraInfo => { println("httpCode: " + ExtraInfo.response.statusCode.getOrElse(0) + "\nResponse: " + ExtraInfo.response.body.string); List(ExtraInfo.response.statusCode, ExtraInfo.response.body.string) })Jacquline
I have another simple question. If I have ` .check(regex("...").saveAs("token")))`, how can I print token value? Thank you in advance.Jacquline
The above mentioned config doesn't work with Gatling 3.0, the appender names have changed. Can't remember the new appender names off the top of my head, but I can look it up later tonight/tomorrow and update the example in the answer.Oleomargarine
It works @iirekkm. Just make sure you are referring to appender you have defined <appender-ref ref="ERROR"/> refers to <appender name="ERROR" ...Eureka
H
33

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.

Harbinger answered 15/10, 2018 at 18:11 Comment(0)
A
16

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>
Advertise answered 27/10, 2017 at 8:14 Comment(4)
Thank you, Kim. That was helpful. I found the following simple programming solution by adding extraInfoExtractor val httpProtocol = http .baseURL(baseURL) .userAgentHeader("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36") .extraInfoExtractor(ExtraInfo => { println("httpCode: " + ExtraInfo.response.statusCode.getOrElse(0) + "\nResponse: " + ExtraInfo.response.body.string); List(ExtraInfo.response.statusCode, ExtraInfo.response.body.string) })Jacquline
I have another simple question. If I have ` .check(regex("...").saveAs("token")))`, how can I print token value? Thank you in advance.Jacquline
The above mentioned config doesn't work with Gatling 3.0, the appender names have changed. Can't remember the new appender names off the top of my head, but I can look it up later tonight/tomorrow and update the example in the answer.Oleomargarine
It works @iirekkm. Just make sure you are referring to appender you have defined <appender-ref ref="ERROR"/> refers to <appender name="ERROR" ...Eureka
K
4

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.

Kallick answered 29/8, 2019 at 13:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.