# 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

**URL:** <https://community.gatling.io/t/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/1909>\
**Category:** Gatling (Open-Source)\
**Created:** [January 15, 2015, 11:18pm UTC](https://community.gatling.io/t/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/1909 "2015-01-15T23:18:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Umesh\_Sundareshan](https://avatars.discourse-cdn.com/v4/letter/u/b2d939/32.png) [@Umesh\_Sundareshan](https://community.gatling.io/u/Umesh_Sundareshan)\
**Post date:** [January 15, 2015, 11:18pm UTC](https://community.gatling.io/t/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/1909/1 "2015-01-15T23:18:41Z")

</div>

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](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](https://abc.com)”)

val headers\_10 = Map(  
“Accept-Encoding” → “gzip, deflate”,  
“Origin” → “[https://cba](https://cba)”)

val headers\_17 = Map(“Accept” → “image/webp,_/_;q=0.8”)

val uri1 = “[www.gstatic.com](http://www.gstatic.com)”  
val uri2 = “[https://cba:443](https://cba:443)”  
val uri3 = “[http://abc/simplesamlphpms4/module.php](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)  
}

---

<div class="post-metadata">

**Author:** ![Abhinav\_Gogna](https://avatars.discourse-cdn.com/v4/letter/a/c0e974/32.png) [@Abhinav\_Gogna](https://community.gatling.io/u/Abhinav_Gogna)\
**Post date:** [January 16, 2015, 1:42pm UTC](https://community.gatling.io/t/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/1909/2 "2015-01-16T13:42:08Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Umesh\_Sundareshan](https://avatars.discourse-cdn.com/v4/letter/u/b2d939/32.png) [@Umesh\_Sundareshan](https://community.gatling.io/u/Umesh_Sundareshan)\
**Post date:** [January 16, 2015, 10:31pm UTC](https://community.gatling.io/t/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/1909/3 "2015-01-16T22:31:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Abhinav\_Gogna](https://avatars.discourse-cdn.com/v4/letter/a/c0e974/32.png) [@Abhinav\_Gogna](https://community.gatling.io/u/Abhinav_Gogna)\
**Post date:** [January 17, 2015, 12:42am UTC](https://community.gatling.io/t/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/1909/4 "2015-01-17T00:42:17Z")

</div>

Umesh,

Sample script and tutorial - [http://gatling.io/docs/2.1.3/quickstart.html#gatling-scenario-explained](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](http://gatling.io/docs/2.1.3/http/http_check.html#http-response-body)

Abhinav

---

<div class="post-metadata">

**Author:** ![Umesh\_Sundareshan](https://avatars.discourse-cdn.com/v4/letter/u/b2d939/32.png) [@Umesh\_Sundareshan](https://community.gatling.io/u/Umesh_Sundareshan)\
**Post date:** [January 19, 2015, 7:19pm UTC](https://community.gatling.io/t/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/1909/5 "2015-01-19T19:19:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Abhinav\_Gogna](https://avatars.discourse-cdn.com/v4/letter/a/c0e974/32.png) [@Abhinav\_Gogna](https://community.gatling.io/u/Abhinav_Gogna)\
**Post date:** [January 20, 2015, 12:08am UTC](https://community.gatling.io/t/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/1909/6 "2015-01-20T00:08:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Vinod\_Kumar\_Jeyaram](https://avatars.discourse-cdn.com/v4/letter/v/e36b37/32.png) [@Vinod\_Kumar\_Jeyaram](https://community.gatling.io/u/Vinod_Kumar_Jeyaram)\
**Post date:** [October 22, 2018, 11:36am UTC](https://community.gatling.io/t/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/1909/7 "2018-10-22T11:36:28Z")

</div>

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
