how to use a correlated value in next json request

Hi All,

i have captured a jwt token and massaged it to get a parameter from it. I want to use this parameter in the next json request but it doesn’t resolve the variable as in below format.

Below is the way I am capturing the variable (roomsourceid) from thw jwt token (token1)

val decodedToken:String = JWSObject.parse(token1).getPayload.toString
roomSourceid = JsonPath.from(decodedToken).getString(“roomSourceId”)

Next request json :

.body(StringBody(
“”"{

“sample1”: “${roomSourceid}”,
“sample2”: “${roomSourceid}”

}“”".stripMargin)).asJson

The roomsourceid in the above json is not getting resolved. Any help appreciated. thank you !

Hello,

you shall be using session if you want to share any data between your requests, for example:

  1. Get and save the access_token received from authenticate url within the session:
private def authenticateUser: ChainBuilder = exec(
http("Get bearer")
.post(s"https://authencateUrl.com")
.basicAuth(session => "login", session => "password")
.check(status.is(200))
.check(jsonPath("$.access_token").find.saveAs("access_token")
  1. Use the saved access token in another request:
private def uploadFile: ChainBuilder = exec(
exec(
http("Upload file")
.post("http://saveMyFile.com")
.header("Authorization", "Bearer ${access_token}")
.check(status.is(201))
)
)
  1. Coordinate requests within the scenario:
private def uploadFileScenario(shallRevokeTokenForEachUser: Boolean): ScenarioBuilder =
scenario(s"Get bearer and upload file").exec(
authenticateAndSaveToken,
uploadFile
)

I hope it helps you,
Daniil

Thanks Daniil,

In the above example, we are capturing the value using .check and that is possible. In my case, i am saving the access_token, massaging it (decode the jwt and pick a variable like roomSourceId from it) and then I am trying to use it in next request. In this way, its not resolving when i use ${ roomSourceId}, i am able to print this in a exec session, but cannot use it in the request.

Please share the full code. You’re most likely not properly storing the extracted data back into the Session.

Hello Stephane,

Please find the code below.

.exec(http(“Request-1”)
.options(“/domain/api/call?locale=en”)
.headers(headers_5)
.resources(http(“Request-2”)
.post(“/domain/api/call?locale=en”)
.headers(headers_12)
.body(RawFileBody(“Data/8_request.json”)).check(status.is(201))
.check(regex(“leftboundary(.*?)&rightboundary”).saveAs(“token”)))) //this token is jwt
.exec({ session =>
val token1 = session(“token”).as[String]
val decodedToken:String = JWSObject.parse(token1).getPayload.toString
roomid = JsonPath.from(decodedToken).getString(“roomSourceId”)
println(“Room source id is” +roomSourceid)
session
})
.exec(http(“Request-3”)
.post(“/url to hit”)
.headers(headers_40)
.body(StringBody(
“”"{

“roomSourceId”: ${roomSourceid},
“preferredPhone” : “800-888-8888”,
}“”".stripMargin))
.check(status is 200))

In Request-3, the value of roomSourceid is not getting resolved. Its getting printed fine in the previous step, i am sure i am missing something with the variable/session. Appreciate your help !!

Either what you’ve provided is not the actual code you’re using, or this code is completely broken.

roomid = JsonPath.from(decodedToken).getString(“roomSourceId”)

This means you’ve defined roomId elsewhere as a var, meaning a global shared variable that all the virtual users will share and try to update without any concurrent access protection.

What you must do is put the decoded value back into the Session:

.exec({ session =>
val token1 = session(“token”).as[String]

val decodedToken:String = JWSObject.parse(token1).getPayload.toString

val roomSourceid = JsonPath.from(decodedToken).getString(“roomSourceId”)
println(“Room source id is” +roomSourceid)
session.set(“roomSourceid”, roomSourceid)
})

Thank you so much Stephane. Yes, you are right, I messed up with the variables. thank you for pointing it out.