Using JSON Responses in Follow-Up Requests

Hello all,

I have been working on this problem for over a week. I’m not sure what I am missing, but I’m sure I’ll feel dumb once the solution is brought to light. Basic scenario is I am trying to load test an API. Eventually I will be testing a whole slew of requests, but for now, I just want to get the first two working.

The first request is just a simple login request, which I have been successful with, but the first request elicits a response with a session token. I need to extract this token, and call it in the next JSON request. I have tried utilizing the .saveAs method, but once I get it saved, I cannot call the value of the token anywhere but inside the session. I have tried to find a way to pass the value outside the session to no avail. Please find my script below, and let me know if you have any idea what I have done wrong.

object RunTest {

val login = exec(http(“Begin_Test”)
.post("/Path/To/Request/")
.body(StringBody("""{“logintype”:“user”,
“user”:“user.name”,
“password”:“Summer2017”,
“host”:“111.0.0.10”
}"""))
.transformResponse{
case response if response.isReceived =>
new ResponseWrapper(response) {
val stripped = response.body.string
println("*** stripped = " + stripped)
override val body = new StringResponseBody(stripped, response.charset)
}
}
.asJSON
.check(jsonPath("$…result").is(“true”))
.check(jsonPath("$…session")
.find
.saveAs(“currentSession”)))

val sessionToken = exec(session => {
session(“currentSession”).as[String]
session})

val addUser = exec(http(“Add_User”)
.post("/Path/To/Second/Request/")
.body(StringBody(s"""{“action”:“ADDUSER”,
“controlid”:“ID”,
“session”:"${sessionToken}",
“username”:“UserName”,
“description”:“Test user”,
“fullname”:“John Doe”,
email":"jdoe@email.com”,
“msg_svc”:"",
“msg_phone”:“8001234567”
}"""))
.asJSON
.check(jsonPath("$…result").is(“true”)))

I have been through a wide variety of errors, but looking at error logs, I see that my main problem is that the session is not getting written correctly. I continually get some gibberish that looks like the code to retrieve the session token. In the Command Prompt, the error I usually get is simply that “false was found where true was expected.”

Any help is appreciated, and thank you very much.

Rob

Hello Rob.

Your first code segment is extracting the token with check(jsonPath("$…session").find.saveAs(“currentSession”))), saving the value in the session with the key currentSession.

Your second code segment is just setting a Scala variable (ok, val) to this same value, but it is not changing the session in any way (ie. creating another session attribute). This is where your problem lies.

You should still be using the currentSession key in your third code segment/second request, not sessionToken, which, as stated, is not a session attribute.

Thank you for your response Nige. I see what you mean, and as I mentioned, I have tried a lot of different things, including referencing “currentSession” in the third part of the code. This was just the most recent iteration. The early steps did not have the second portion of code at all. That was an attempt to extract the value of “currentSession” out of the session for use in other areas, but alas, that did not work either.

Could it be the way I tried to reference “currentSession”?

I did attempt it as:

“session”:“currentSession”
“session”:"${currentSession}"
“session”:“session(“currentSession).as[String]”
“session”:”${session(“currentSession).as[String]}”

For sake of brevity, I will just say, I tried every combination of these listed above I could think of. Putting the ‘$’ and ‘{}’ around just “currentSession”, both with and without the " " and both inside and outside of them. Is there something I’m missing???

Thanks again for any help!

Rob

Rob, I’m only a couple of months into Scala and Gatling myself, so this sort of thing isn’t a distant memory for me. :slight_smile:

Your second usage, ie. “session”:"${currentSession}", is the correct one for your case.

However, I’m thinking it might not have worked for you because of your use of both triple-quotes, and the string interpolator (the character ‘s’ preceding the triple-quotes). My suggestion is to delete the ‘s’ (highlighted in red below):

val addUser = exec(http(“Add_User”)
.post("/Path/To/Second/Request/")

.body(StringBody(s"""{“action”:“ADDUSER”,

“session”:"${currentSession}",

}"""))
.asJSON
.check(jsonPath("$…result").is(“true”)))

This means you’ll be passing a properly-formed Gatling EL String to StringBody (ref. http://gatling.io/docs/current/http/http_request/), which will/should reference the currentSession attribute, and work as you expect.

Thank you Nige!!! That worked perfectly. At least to solve the problem I thought was creating my errors. Now back to the drawing board to figure out the next problem with this code. Once again, Thanks!!!

Rob