How do I capture the response body of a Get call (which is actually a redirect to another URL) into a variable and print it in Gatling


I am new to Gatling and did a lot of Googling, but couldn’t make it work. Here’s what I am trying to do.

  1. I am making a Get request to a URL, the response body has another URL (which is actually a redirect) and few key-value pairs.
  2. I am trying to save that response body into a variable, so that I can extract the key-value pairs.

Here’s the script, but somehow it just prints nothing.

It would be great help, if somebody can help get this script working.


import scala.concurrent.duration._

import io.gatling.core.Predef._
import io.gatling.http.Predef._
import io.gatling.jdbc.Predef._

class SS3 extends Simulation {

val httpProtocol = http
.inferHtmlResources(BlackList("""..js""", “”"..css""", “”"..gif""", “”"..jpeg""", “”"..jpg""", “”"..ico""", “”"..woff""", “”"..(t|o)tf""", “”"..png"""), WhiteList())
.acceptEncodingHeader(“gzip, deflate, sdch”)
.userAgentHeader(“Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36”)

val headers_4 = Map(“Pragma” → “no-cache”)

val headers_8 = Map(
“Accept” → “application/json, text/plain, /”,
“Accept-Encoding” → “gzip, deflate”,
“Origin” → “”)

val headers_10 = Map(
“Accept-Encoding” → “gzip, deflate”,
“Origin” → “https://cba”)

val headers_17 = Map(“Accept” → “image/webp,/;q=0.8”)

val uri1 = “
val uri2 = “https://cba:443
val uri3 = “http://abc/simplesamlphpms4/module.php
//val Res1 = “”
//val Res3 = “”
//val Res4 = “”

val scn = scenario(“SS3”)
.exec {session =>
//val Res1: String = session.getTypedAttribute(“Res1”)
println("Res1=> " + Res1)


You have disabled redirect globally. You have to do that manually in this case.

I recommend you start with removing disable redirect. Then log all the requests/responses - check logback.xml.

Then use css selector or regex parser to capture your key value pairs from response.

Hi Abhinav,

Thanks for replying.

I have captured all the requests and responses in a text document. I am new to Scala language, the problem that I have is with the APIs / keywords that I need to use to do this and the structure I need to follow. It would be of great help if you or anyone can share a sample Scala script which does something similar.



Sample script and tutorial -

Looking at your script, I am going to make a supposition. What you really want to do is parse some information out of the html page and that information maybe used in subsequent gatling requests and so forth.

For parsing html, you can use css selector or regex parser.


Hi Abhinav,

I used headerRegex and was able to extract the values that I wanted. Now, I am stuck with another issue. There’s a .resources call which has 2 http requests (request_1 and request_2) and request_2 uses a dynamic key-value pair extracted from request_1. But, during script execution what is happening is, request_2 gets executed even before request_1). Since they are inside a .resources call, probably it’s happening that way. So, I took them out of .resources call and made them as 2 separated http requests, but with that the referer URL of second request (i.e., request_2) got changed and I am getting a ‘400 bad request’ error from server.

So, I need to fix them either by making sure that request_1 gets executed before request_2, if they are inside .resources call or I should be able to forcefully change the referer of ‘request_2’ using Gatling.

Do you think I can do any of these?



Inside resources, there is no guarantee which request gets executed first. You may have to find another way of doing it. Forcefully changing referrer - not sure but if it works then until you can find another solution maybe the only way right now.


Hi Umesh/Abhinav,

I am working on a similar scenario.
I am curious whether you found a solution for this ? If yes please share it.