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

Vladimir picture Vladimir · Oct 27, 2017 · Viewed 19.1k times · Source

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

Answer

Paulo Merson picture Paulo Merson · Oct 15, 2018

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.