# Retrieve the value of a variable after http post

**URL:** <https://community.gatling.io/t/retrieve-the-value-of-a-variable-after-http-post/1063>\
**Category:** Gatling (Open-Source)\
**Created:** [May 30, 2014, 7:20pm UTC](https://community.gatling.io/t/retrieve-the-value-of-a-variable-after-http-post/1063 "2014-05-30T19:20:06Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pandi](https://avatars.discourse-cdn.com/v4/letter/p/e19b73/32.png) [@Pandi](https://community.gatling.io/u/Pandi)\
**Post date:** [May 30, 2014, 7:20pm UTC](https://community.gatling.io/t/retrieve-the-value-of-a-variable-after-http-post/1063/1 "2014-05-30T19:20:06Z")

</div>

I am trying to use the value returned in a SOAP response and use it in the subsequent POSTs.

import io.gatling.core.Predef.\_  
import io.gatling.core.session.Expression  
import io.gatling.http.Predef.\_  
import io.gatling.jdbc.Predef.\_  
import io.gatling.http.Headers.Names.\_  
import io.gatling.http.Headers.Values.\_  
import scala.concurrent.duration.\_  
import bootstrap.\_  
import assertions.\_

class GetPriceDetail extends Simulation {

//removed code…

.exec(  
http(“LogSession”)  
.post(“[http://IPAddress/service.svc](http://IPAddress/service.svc)”)  
.headers(loginHeader)  
.body(StringBody(soapLoginReq))  
//.check(status.is(200))

.check(xpath("//SessionService:SessionToken", List(“SessionService” → “[http://schemas.datacontract.org/2014/services.Contract.SessionService")).saveAs("sessionTkn](http://schemas.datacontract.org/2014/services.Contract.SessionService%22)).saveAs(%22sessionTkn)”))  
)  
println(s"Session Token: " + “$sessionTkn” )

}

With TRACE turned on, I can see the session token is received, but println() does not print the value, instead it prints “$sessionTkn”.

I am using Gatling 2.0 M3a.

How can I ‘store’ and ‘use’ the value sessionTkn in a subsequent call to POST?  
Any help is greatly appreciated.

Thanks,  
Pandi

---

<div class="post-metadata">

**Author:** ![Shawn\_Clark](https://avatars.discourse-cdn.com/v4/letter/s/ecb155/32.png) [@Shawn\_Clark](https://community.gatling.io/u/Shawn_Clark)\
**Post date:** [May 30, 2014, 7:50pm UTC](https://community.gatling.io/t/retrieve-the-value-of-a-variable-after-http-post/1063/2 "2014-05-30T19:50:49Z")

</div>

Not looking too deeply into it but it looks like you have your println incorrect. Here is what I am guessing you wanted:

println(s"Session Token: $sessionTkn")

The s"" makes it an interpolated string ([http://docs.scala-lang.org/overviews/core/string-interpolation.html](http://docs.scala-lang.org/overviews/core/string-interpolation.html)) so you don’t need to concat the value to the end.

---

<div class="post-metadata">

**Author:** ![Pandi](https://avatars.discourse-cdn.com/v4/letter/p/e19b73/32.png) [@Pandi](https://community.gatling.io/u/Pandi)\
**Post date:** [May 30, 2014, 8:37pm UTC](https://community.gatling.io/t/retrieve-the-value-of-a-variable-after-http-post/1063/3 "2014-05-30T20:37:01Z")

</div>

Shawn,  
Thanks for the response. If I use  
println("Session Token: “$sessionTkn”)

I get the below result:  
Simulation M2MMC.GetDeviceInfo started…  
Session Token: $sessionTkn

\< deleted part of output \>

---

<div class="post-metadata">

**Author:** ![Nicolas\_R](https://avatars.discourse-cdn.com/v4/letter/n/f14d63/32.png) [@Nicolas\_R](https://community.gatling.io/u/Nicolas_R)\
**Post date:** [May 30, 2014, 8:47pm UTC](https://community.gatling.io/t/retrieve-the-value-of-a-variable-after-http-post/1063/4 "2014-05-30T20:47:30Z")

</div>

Your println() function isn’t called in a Gatling “context”.  
For doing so, you have to encapsulate it in an .exec() call like this:

.exec( session =\>println("Session Token: " + session(“sessionTkn”))  
session  
)

Notice that .exec() takes a Function[Session, Session] as argument and that the session is immutable.

regards  
Nicolas

---

<div class="post-metadata">

**Author:** ![Pandi](https://avatars.discourse-cdn.com/v4/letter/p/e19b73/32.png) [@Pandi](https://community.gatling.io/u/Pandi)\
**Post date:** [May 30, 2014, 10:57pm UTC](https://community.gatling.io/t/retrieve-the-value-of-a-variable-after-http-post/1063/5 "2014-05-30T22:57:35Z")

</div>

All, Thanks again for reading and helping.

After much researching the Forum and Gatling 2 documentation, I think, I got it working, at least I can see the correct “sessionTkn” value printed after the first POST-followed-by-saveAs. Hopefully the code example will help other newbies like me.

import io.gatling.core.Predef.\_  
import io.gatling.core.session.Expression  
import io.gatling.http.Predef.\_  
import io.gatling.jdbc.Predef.\_  
import io.gatling.http.Headers.Names.\_  
import io.gatling.http.Headers.Values.\_  
import scala.concurrent.duration.\_  
import bootstrap.\_  
import assertions.\_

class GetPriceDetail extends Simulation {  
val scn = scenario(“LoginAndDevInfo”)

.exec(  
session =\> {  
session.set(“sessionTkn”, “dummyValue”)  
println("Session information: " + session) //for debug #1  
session  
}  
)

//removed code…

.exec(  
http(“LogSession”)  
.post(“[http://IPAddress/service.svc](http://IPAddress/service.svc)”)  
.headers(loginHeader)  
.body(StringBody(soapLoginReq))  
//.check(status.is(200))

.check(xpath("//SessionService:SessionToken", List(“SessionService” → “[http://schemas.datacontract.org/2014/services.Contract.SessionService")).saveAs("sessionTkn](http://schemas.datacontract.org/2014/services.Contract.SessionService%22)).saveAs(%22sessionTkn)”))  
)

.exec(session =\>{  
println(“Debug print#2”)  
println(session(“sessionTkn”).as[String]) \<\<\< this one prints the correct value for sessionTkn from the previous POST  
session  
})

setUp(scn.inject(ramp(1 users) over (10 seconds)))  
}

Thanks  
Pandi

---

<div class="post-metadata">

**Author:** ![Pratik](https://avatars.discourse-cdn.com/v4/letter/p/4bbf92/32.png) [@Pratik](https://community.gatling.io/u/Pratik)\
**Post date:** [May 30, 2018, 12:49pm UTC](https://community.gatling.io/t/retrieve-the-value-of-a-variable-after-http-post/1063/6 "2018-05-30T12:49:39Z")

</div>

This post helped me …
