# Gatling/Scala is unable to find JSON retrieved session key

**URL:** <https://community.gatling.io/t/gatling-scala-is-unable-to-find-json-retrieved-session-key/1359>\
**Category:** Gatling (Open-Source)\
**Created:** [September 4, 2014, 1:58pm UTC](https://community.gatling.io/t/gatling-scala-is-unable-to-find-json-retrieved-session-key/1359 "2014-09-04T13:58:09Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bogdan\_Tudosie](https://avatars.discourse-cdn.com/v4/letter/b/cc9497/32.png) [@Bogdan\_Tudosie](https://community.gatling.io/u/Bogdan_Tudosie)\
**Post date:** [September 4, 2014, 1:58pm UTC](https://community.gatling.io/t/gatling-scala-is-unable-to-find-json-retrieved-session-key/1359/1 "2014-09-04T13:58:09Z")

</div>

I am very new to Gatling and being half made to use it I need to write automation scripts with it. I have no prior experience with it in any way.

When I am trying to run multiple scenarios I retrieve a “session\_key” as part of a JSON response after a login. I tried to follow guides from the internet, saved this value but when I attempt to use it further in my second scenario the Expression Language can’t find it.

I have verified my JSONPath expression for it and it seems to be correct.

```auto
{"result":1,"resultType":"ack","output":{"session_key":"a value for the session key goes here"},"errorMessage":false,"errorCode":false,"additional":{"locationtabs":["tab 1","tab 2","tab 3"]}}

```

I have also listed my code below but replaced usage data with mock data for my question.

```auto
package basic

import com.excilys.ebi.gatling.core.Predef._
import com.excilys.ebi.gatling.http.Predef._
import com.excilys.ebi.gatling.jdbc.Predef._
import com.excilys.ebi.gatling.http.Headers.Names._
import com.jayway.jsonpath._
import akka.util.duration._
import bootstrap._

class GetLocations extends Simulation
{

    val httpConf = httpConfig
        .baseURL("https://myURL")
        .acceptCharsetHeader("ISO-8859-1,utf-8;q=0.7,*;q=0.7")
        .acceptHeader("text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
        .acceptEncodingHeader("gzip, deflate")
        .acceptLanguageHeader("fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3")
        .disableFollowRedirect

    val scn = scenario("Login Test")
        .exec(
            http("requestLogin")
            .post("/restapi/login")
            .param("username", "a username")
            .param("password", "a password")
            .param("client_uid", "testUid")
            .check(jsonPath("$.result").is("1"))
            .check(jsonPath("$.output.session_key").findAll.saveAs("session_key"))

        ).pause(15)

    val getLocations = scenario("Get Locations")
        .exec(
            http("getLocations")
            .post("/restapi/get_employee_favouritelocations")
            .param("client_uid","testUid")
            .param("clientType","automatedTest")
            .param("session_key","${session_key}")
            .check(jsonPath("$.result").is("1"))
        )

    setUp(scn.users(50).ramp(50).protocolConfig(httpConf),
          getLocations.users(50).ramp(50).protocolConfig(httpConf)
        )

}

```

Any help you can provide is appreciated as this has been giving me headaches for a week and unfortunately only i am available to work with this currently.

---

<div class="post-metadata">

**Author:** ![Excilys](https://avatars.discourse-cdn.com/v4/letter/e/8797f3/32.png) [@Excilys](https://community.gatling.io/u/Excilys)\
**Post date:** [September 4, 2014, 2:05pm UTC](https://community.gatling.io/t/gatling-scala-is-unable-to-find-json-retrieved-session-key/1359/2 "2014-09-04T14:05:18Z")

</div>

You don’t get it right!

Here, you have 2 different scenarios, each running 50 users: the first 50 ones save session\_key into THEIR session and the 50 others try to find it in THEIR own, and of course it doesn’t exist.

I don’t understand what you’re trying to simulate.

---

<div class="post-metadata">

**Author:** ![Bogdan\_Tudosie](https://avatars.discourse-cdn.com/v4/letter/b/cc9497/32.png) [@Bogdan\_Tudosie](https://community.gatling.io/u/Bogdan_Tudosie)\
**Post date:** [September 5, 2014, 1:39pm UTC](https://community.gatling.io/t/gatling-scala-is-unable-to-find-json-retrieved-session-key/1359/3 "2014-09-05T13:39:16Z")

</div>

In short, I did not get this right in the beginning. I am moving slowly onto the right track, doing two chain .exec calls for each step (rolled two scenarios into one).

However it seems that even in the case of one scenario the Session Key is either not saved or when i reuse it the variable is not correct.

The simulation basically does as follows:

1. Login
2. If the login is a success then verify if the user has any locations (GetLocations step).

Below you will see the updated code.

package basic

import com.excilys.ebi.gatling.core.Predef.\_  
import com.excilys.ebi.gatling.http.Predef.\_  
import com.excilys.ebi.gatling.jdbc.Predef.\_  
import com.excilys.ebi.gatling.http.Headers.Names.\_  
import com.jayway.jsonpath.\_  
import akka.util.duration.\_  
import bootstrap.\_

class GetLocations extends Simulation  
{

val httpConf = httpConfig  
.baseURL(“[https://URL](https://URL)”)  
.acceptCharsetHeader(“ISO-8859-1,utf-8;q=0.7,_;q=0.7")  
.acceptHeader("text/html,application/xhtml+xml,application/xml;q=0.9,_/\*;q=0.8”)  
.acceptEncodingHeader(“gzip, deflate”)  
.acceptLanguageHeader(“fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3”)  
.disableFollowRedirect

val scn = scenario(“GetLocations”)  
.exec(  
http(“requestLogin”)  
.post("/restapi/login")  
.param(“username”, “user”)  
.param(“password”, “pwd”)  
.param(“client\_uid”, “testUid”)  
.check(jsonPath("$.result").is(“1”), jsonPath("$.output.session\_key").findAll.saveAs(“session\_key”))

).pause(15)

.exec(  
http(“getLocations”)  
.post("/restapi/get\_employee\_favouritelocations")  
.param(“client\_uid”,“testUid”)  
.param(“clientType”,“automatedTest”)  
.param(“session\_key”,"${session\_key}")  
.check(jsonPath("$.result").is(“1”))  
)

setUp(scn.users(50).ramp(50).protocolConfig(httpConf))

}

Thank you,  
Bogdan

---

<div class="post-metadata">

**Author:** ![Excilys](https://avatars.discourse-cdn.com/v4/letter/e/8797f3/32.png) [@Excilys](https://community.gatling.io/u/Excilys)\
**Post date:** [September 5, 2014, 1:43pm UTC](https://community.gatling.io/t/gatling-scala-is-unable-to-find-json-retrieved-session-key/1359/4 "2014-09-05T13:43:35Z")

</div>

Are you sure you want to use findAll?  
findAll will save a Seq (like a java Java), so as you then pass it to param, it get serialized as a String, which is probably not what you want.

I’m almost sure you shouldn’t be using findAll (I don’t expect a session\_key to be multivalued). If so, remove it.

---

<div class="post-metadata">

**Author:** ![Bogdan\_Tudosie](https://avatars.discourse-cdn.com/v4/letter/b/cc9497/32.png) [@Bogdan\_Tudosie](https://community.gatling.io/u/Bogdan_Tudosie)\
**Post date:** [September 8, 2014, 6:03am UTC](https://community.gatling.io/t/gatling-scala-is-unable-to-find-json-retrieved-session-key/1359/5 "2014-09-08T06:03:46Z")

</div>

I suppose a find() call would find a single value?

---

<div class="post-metadata">

**Author:** ![Bogdan\_Tudosie](https://avatars.discourse-cdn.com/v4/letter/b/cc9497/32.png) [@Bogdan\_Tudosie](https://community.gatling.io/u/Bogdan_Tudosie)\
**Post date:** [September 8, 2014, 6:11am UTC](https://community.gatling.io/t/gatling-scala-is-unable-to-find-json-retrieved-session-key/1359/6 "2014-09-08T06:11:15Z")

</div>

Sorry for the double reply. my JSON response is as follows:

{“result”:1,“resultType”:“ack”,“output”:{“session\_key”:“session\_key\_whatever\_it\_is”},“errorMessage”:false,“errorCode”:false,“additional”:{“locationtabs”:[“closest”,“favourites”]}}

Hope it will help more, perhaps i got my jsonpath notation wrong.

Bogdan

---

<div class="post-metadata">

**Author:** ![Excilys](https://avatars.discourse-cdn.com/v4/letter/e/8797f3/32.png) [@Excilys](https://community.gatling.io/u/Excilys)\
**Post date:** [September 8, 2014, 7:50am UTC](https://community.gatling.io/t/gatling-scala-is-unable-to-find-json-retrieved-session-key/1359/7 "2014-09-08T07:50:30Z")

</div>

> I suppose a find() call would find a single value?

[http://gatling.io/docs/2.0.0-RC3/http/http\_check.html?highlight=check#extracting](http://gatling.io/docs/2.0.0-RC3/http/http_check.html?highlight=check#extracting)

Did you actually tried to use find?

Then, you really should upgrade to Gatling 2 current release candidates. Gatling 1 will ne longer be maintained.

---

<div class="post-metadata">

**Author:** ![Bogdan\_Tudosie](https://avatars.discourse-cdn.com/v4/letter/b/cc9497/32.png) [@Bogdan\_Tudosie](https://community.gatling.io/u/Bogdan_Tudosie)\
**Post date:** [September 9, 2014, 7:23am UTC](https://community.gatling.io/t/gatling-scala-is-unable-to-find-json-retrieved-session-key/1359/8 "2014-09-09T07:23:26Z")

</div>

I already have Gatling 2.0. I downloaded a release candidate.

---

<div class="post-metadata">

**Author:** ![Excilys](https://avatars.discourse-cdn.com/v4/letter/e/8797f3/32.png) [@Excilys](https://community.gatling.io/u/Excilys)\
**Post date:** [September 9, 2014, 7:26am UTC](https://community.gatling.io/t/gatling-scala-is-unable-to-find-json-retrieved-session-key/1359/9 "2014-09-09T07:26:13Z")

</div>

The sample you provided in your first post was for Gatling 1, hence my suggestion.

---

<div class="post-metadata">

**Author:** ![Bogdan\_Tudosie](https://avatars.discourse-cdn.com/v4/letter/b/cc9497/32.png) [@Bogdan\_Tudosie](https://community.gatling.io/u/Bogdan_Tudosie)\
**Post date:** [September 9, 2014, 7:45am UTC](https://community.gatling.io/t/gatling-scala-is-unable-to-find-json-retrieved-session-key/1359/10 "2014-09-09T07:45:13Z")

</div>

Maybe I am not getting it: tried to use ‘find’. I have tried without as well. Perhaps I am just not getting it.

---

<div class="post-metadata">

**Author:** ![Bogdan\_Tudosie](https://avatars.discourse-cdn.com/v4/letter/b/cc9497/32.png) [@Bogdan\_Tudosie](https://community.gatling.io/u/Bogdan_Tudosie)\
**Post date:** [September 9, 2014, 8:15am UTC](https://community.gatling.io/t/gatling-scala-is-unable-to-find-json-retrieved-session-key/1359/11 "2014-09-09T08:15:15Z")

</div>

My scenario code is as follows:

val scn = scenario(“GetLocations”)  
.exec(  
http(“requestLogin”)  
.post("/restapi/login")  
.param(“username”, “username”)  
.param(“password”, “pwd”)  
.param(“client\_uid”, “testUid”)  
.check(jsonPath("$.result").is(“1”), jsonPath("$.output.session\_key").find.saveAs(“session\_key”))

).pause(15)

.exec(  
http(“getLocations”)  
.post("/restapi/get\_employee\_favouritelocations")  
.param(“client\_uid”,“testUid”)  
.param(“clientType”,“automatedTest”)  
.param(“session\_key”,"${session\_key}")  
.check(jsonPath("$.result").is(“1”))  
)

Perhaps I am calling the ‘saveAs’ wrong. I am unsure how to deal with it at this point. As mentioned this is the first time I used Gatling so everything is very confusing. I am simply trying to follow the documentation.

---

<div class="post-metadata">

**Author:** ![Excilys](https://avatars.discourse-cdn.com/v4/letter/e/8797f3/32.png) [@Excilys](https://community.gatling.io/u/Excilys)\
**Post date:** [September 9, 2014, 8:20am UTC](https://community.gatling.io/t/gatling-scala-is-unable-to-find-json-retrieved-session-key/1359/12 "2014-09-09T08:20:40Z")

</div>

Which release candidate do you use exactly?  
What’s your problem exactly, now?

I have several comments:

- Having a webservice named “getLocations” that has to be used with POST (instead of GET) doesn’t look very restful to me. Are you sure of this?
- If you’re really doing a POST, are you sure your service implementation doesn’t expect some special headers, such as Content-Type: “application/x-www-form-urlencoded”?

---

<div class="post-metadata">

**Author:** ![Bogdan\_Tudosie](https://avatars.discourse-cdn.com/v4/letter/b/cc9497/32.png) [@Bogdan\_Tudosie](https://community.gatling.io/u/Bogdan_Tudosie)\
**Post date:** [September 9, 2014, 8:47am UTC](https://community.gatling.io/t/gatling-scala-is-unable-to-find-json-retrieved-session-key/1359/13 "2014-09-09T08:47:37Z")

</div>

Stephane,

The Webservice in question uses post. I did not develop it I simply have to test it.

My problem is I simply want to extract the value of the session\_key out of the JSON response and reuse it in the next step of the test. It functions as follows:

1. User logs in, gets a session key
2. Locations are retrieved using that session key to access further calls as the user needs.
3. Locations are returned. If the user has any locations, the “result” will be 1. If none it will be 0.

That is what I am trying to get to. Checking if the result is 1 is easy. Saving the session\_key seems the hard part for me.

Bogdan

PS: Thanks again for your patience.

---

<div class="post-metadata">

**Author:** ![Excilys](https://avatars.discourse-cdn.com/v4/letter/e/8797f3/32.png) [@Excilys](https://community.gatling.io/u/Excilys)\
**Post date:** [September 9, 2014, 1:44pm UTC](https://community.gatling.io/t/gatling-scala-is-unable-to-find-json-retrieved-session-key/1359/14 "2014-09-09T13:44:09Z")

</div>

> Saving the session\_key seems the hard part for me.

What’s your problem exactly?  
Your simulation looks fine to me.

---

<div class="post-metadata">

**Author:** ![Bogdan\_Tudosie](https://avatars.discourse-cdn.com/v4/letter/b/cc9497/32.png) [@Bogdan\_Tudosie](https://community.gatling.io/u/Bogdan_Tudosie)\
**Post date:** [September 9, 2014, 4:02pm UTC](https://community.gatling.io/t/gatling-scala-is-unable-to-find-json-retrieved-session-key/1359/15 "2014-09-09T16:02:54Z")

</div>

The problem was solved. The pause of 15 seconds between the calls caused all the trouble. Thank you Stephane!

---

<div class="post-metadata">

**Author:** ![Excilys](https://avatars.discourse-cdn.com/v4/letter/e/8797f3/32.png) [@Excilys](https://community.gatling.io/u/Excilys)\
**Post date:** [September 9, 2014, 4:11pm UTC](https://community.gatling.io/t/gatling-scala-is-unable-to-find-json-retrieved-session-key/1359/16 "2014-09-09T16:11:31Z")

</div>

Funny explanation. Glad you found it! 🙂
