Struggling to access session variable in http check transform

Hi all.

I need to access a session variable in a http transform function called 'check_scope()'.  But the print statement in my check_scope function 
shows that the "${scopeContains}" is not being retrieved from the session

Below is the code and console output.  Other things I've tried:  injecting session into he outer exec, referencing session in the transform call, doing exec with session
to print the session value inside the check_scope function which doesn't print anything oddly.

Any pointers to try, or ideally a code pattern I can look at are appreciated.  Thanks!

**HTTP post that returns 'scope' value to be checked**
.exec(http(OAuthV2TokenReq)
      .post("/oauth/token")
      .basicAuth(AuthConfig.WEB_CLIENT_ID, AuthConfig.WEB_CLIENT_SECRET)
      .headers(HEADERS_LOGIN_OAUTH)
      .formParam("grant_type", "authorization_code")
      .formParam("code", "${code}")
      .formParam("redirect_uri", GlobalVars.getEnv)
      .check(status.is(200))
      .check(jsonPath("$.id").ofType[String].exists)
      .check(jsonPath("$.access_token").ofType[String].exists)

      .check(jsonPath("$.id").saveAs("user_id"))
      .check(jsonPath("$.access_token").saveAs("jwt_token"))
      .check(jsonPath("$.refresh_token").saveAs("refresh_token"))
      .check(jsonPath("$.expires_in").ofType[Int].saveAs("expires_in"))
      .check(jsonPath("$.expires_in").ofType[Int].transform(convertExpiresInToDate(_)).saveAs("jwtExpiryDate"))
 **.check(jsonPath("$.scope").ofType[String].transform(check_scope(_, "${scopeContains}")).saveAs("scope"))**
      .check(jsonPath("$").saveAs("auth_response"))
 )

**check_scope Function:**



```
private def check_scope(scope: String, scopeContains: String) : String = {
```


```
  println("CHECK_SCOPE scope:  " + scope + ", scopeContains:  '" + scopeContains + "'")

```


```
  scope
```


```
}
```

Console output:

Please try:

.check(jsonPath("$.scope").ofType[String]**.transform((scope, session) => check_scope(scope, session("scopeContains").as[String])**.saveAs("scope"))

Reference:
https://gatling.io/docs/current/http/http_check/#transforming

Hi Vu,

Thanks - I tried a variation of what you proposed but nothing printed out, not even CHECK_SCOPE_ENTERING. Here’s the snippet for what I did. I also found something else interesting: “scopeContains” has a value in session before the http call, and doesn’t after: I have code for that below as well.

Thanks again for looking at this.

===> Call to check_scope2

.check(jsonPath("$.scope").ofType[String].transform((scope, session) => check_scope2(scope, session)).saveAs("scope"))

Hi Bruce

Looks like there’s a mixing of Gatling DSL and Scala codes in your functions. Please update and try again:

private def check_scope2(scope: String, session: Session) : String = {

    println("CHECK_SCOPE ENTERING")

    if (session.contains("scopeContains")) {
        println(s"check_scope session:scopeContains:  ${session("scopeContains").as[String]}")
    }

    println("CHECK_SCOPE RETURNING")

    scope
}

And the “after http call” doesn’t look right. You can use the “before http call” format:

  .doIf(session => session.contains("scopeContains")) {
    exec(session => {

println(“oauth/token called”)

You’re right, we use scala as our test language with the gatling framewor,

Thanks - made those changes. Getting a run-time error, I’ll post here in case it’s helpful

Simulation simulations.fvt.permissions.PermissionsValidation started…
13:21:30.438 [ERROR] i.g.c.a.b.SessionHookBuilder$$anon$1 - ‘hook-190’ crashed w
ith ‘java.util.NoSuchElementException: key not found: user_id’, forwarding to th
e next one