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

Hi,

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.

Thanks,
Umesh

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
.baseURL(“http://abc.com”)
.disableFollowRedirect
.inferHtmlResources(BlackList("""..js""", “”"..css""", “”"..gif""", “”"..jpeg""", “”"..jpg""", “”"..ico""", “”"..woff""", “”"..(t|o)tf""", “”"..png"""), WhiteList())
.acceptHeader("text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,
/*;q=0.8")
.acceptEncodingHeader(“gzip, deflate, sdch”)
.acceptLanguageHeader(“en-GB,en-US;q=0.8,en;q=0.6”)
.connection(“keep-alive”)
.contentTypeHeader(“application/x-www-form-urlencoded”)
.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” → “https://abc.com”)

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

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

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

val scn = scenario(“SS3”)
.exec(http(“request_0”)
.get("/simplesamlphpms4/module.php/core/authenticate.php?as=ssg-idp")
//.check(status.is(302),currentLocationRegex("""(.*)""").saveAs(Res1)))
//.check(currentLocation.transform(string=>string).saveAs(“Res1”)))
.check(bodyString.saveAs(“Res1”)))
.exec {session =>
//val Res1: String = session.getTypedAttribute(“Res1”)
println("Res1=> " + Res1)
session
}

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

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.

Thanks,
Umesh

Umesh,

Sample script and tutorial - http://gatling.io/docs/2.1.3/quickstart.html#gatling-scenario-explained

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. http://gatling.io/docs/2.1.3/http/http_check.html#http-response-body

Abhinav

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?

THanks,
Umesh

Umesh,

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.

Abhinav

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.

Thanks,
Vinod