Dynamically generating param values for an API and setting it using session

Hi all

I am struggling to set new value for each session using set session method and pass it to the variable for next call of the same api.

I can see that new values are generated from my functions every time I print the temp params but after API call I see still only first value is passed for all sessions and new values are not assigned.

Is it possible to help ?

are there any documentations for this kind of scenario where we want to pass values without csv.

class  TEST_APP_NEW  extends Simulation {

   /*set value for first time */
    var refreshtoken: String = new OIDCAuthentication.getAccessToken()
   val clientId="1234568990"
   val callbackUrl="http://localhost/test/callback"
   val codeVerifierformat = new OIDCAuthentication.getVerifier()
   val code = new OIDCAuthentication.getCode(codeVerifiersocoformat)
   val codeVerifier = codeVerifierformat.getValue
 
 val httpProtocol = http
    .baseUrl("https://baseurl")
    .inferHtmlResources()
    .acceptHeader("*/*")
    .acceptEncodingHeader("gzip, deflate")
    .acceptLanguageHeader("en-US,en;q=0.5")
    .userAgentHeader("Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:98.0) Gecko/20100101 Firefox/98.0")
    .shareConnections
   
    val header_Stores = Map(
    "Content-Type" -> "application/x-www-form-urlencoded")
     
  val scn = scenario("TEST_APP_NEW")
    .exec ( session => {
               println(s"Code :", code)
               println(s"code_verifier:",codeVerifier)
              session
             })
       .exec(http("TOKENTEST")
      .post("/op/oidc/token")
      .headers(header_Stores)
      .formParam("client_id", s"$clientId")
      .formParam("code", s"$code")
      .formParam("redirect_uri", s"$callbackUrl")
      .formParam("code_verifier",s"$codeVerifier")
      .formParam("grant_type", "authorization_code")
      .check(status.is(200))
      .check(bodyString.saveAs("TOKENS"))
      .check(jsonPath("$.access_token").saveAs("access_token"))
        )
          /*reassigning values for next sessions or requests */
          .exec ( session => {
             val codeVerifierformatter = new OIDCAuthentication.getVerifier()
             val codeNEW = new OIDCAuthentication.getCode(codeVerifierformatter)
             val codeVerifierNEW = codeVerifierformatter.getValue
               session.set("code", codeNEW)
              session.set("codeVerifier", codeVerifierNEW)
              session
             })

Any help would be higly appreciated,

Thanks and Regards
Sathish

You’re not using Session#set properly, please see the warning in the documentation.

Hi @slandelle Thanks for the documentation
I have tried as below
exec { session =>
val codeVerifierformatter = new OIDCAuthentication.getVerifier()
val codeNEW = new OIDCAuthentication.getCode(codeVerifierformatter)
val codeVerifierNEW = codeVerifierformatter.getValue
val newSession = session.setAll((“code” → codeNEW),(“codeVerifier” → codeVerifierNEW))
println(newSession)
newSession
}
But still new values are not passed at all . My first request only pass and rest fails with 400 bad request since the same old values passes for all sessions.

Regards
Sathish

This means you have another issue besides this first one.

There’s no way to help you if you don’t provide a full reproducer as requested here.

Try this…

There are tons that’s wrong in the sample that you provided.

class  TEST_APP_NEW  extends Simulation {

  /*set value for first time */
  val clientId = "1234568990"
  val callbackUrl = "http://localhost/test/callback"

  val httpProtocol = http
    .baseUrl("https://baseurl")
    .inferHtmlResources()
    .acceptHeader("*/*")
    .acceptEncodingHeader("gzip, deflate")
    .acceptLanguageHeader("en-US,en;q=0.5")
    .userAgentHeader("Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:98.0) Gecko/20100101 Firefox/98.0")
    .shareConnections

  val header_Stores = Map(
    "Content-Type" -> "application/x-www-form-urlencoded")

  val sfunc = exec(session => {
    val refreshtoken: String = new OIDCAuthentication.getAccessToken()
    val codeVerifierformat = new OIDCAuthentication.getVerifier()
    val code = new OIDCAuthentication.getCode(codeVerifiersocoformat)
    val codeVerifier = codeVerifierformat.getValue
    session.setAll(("refreshtoken", refreshtoken),
      ("codeVerifier", codeVerifier),
      ("code", code))
  })
  val scn = scenario("TEST_APP_NEW")
    .exec(sfunc)
    .exec(http("TOKENTEST")
      .post("/op/oidc/token")
      .headers(header_Stores)
      .formParam("client_id", clientId)
      .formParam("code", "${code}")
      .formParam("redirect_uri", callbackUrl)
      .formParam("code_verifier", "${codeVerifier}")
      .formParam("grant_type", "authorization_code")
      .check(status.is(200))
      .check(bodyString.saveAs("TOKENS"))
      .check(jsonPath("$.access_token").saveAs("access_token")))
    .exec(sfunc)
}
  

Thanks a lot @shoaib42 , I am trying it now and will get back.

regards
Sathish

I have prepared Example in JAVA how to “transfer” values using session:

package pl.gemiusz;

import io.gatling.javaapi.core.ScenarioBuilder;
import io.gatling.javaapi.core.Session;
import io.gatling.javaapi.core.Simulation;
import io.gatling.javaapi.http.HttpProtocolBuilder;

import static io.gatling.javaapi.core.CoreDsl.*;
import static io.gatling.javaapi.http.HttpDsl.http;
import static io.gatling.javaapi.http.HttpDsl.status;

public class Case0009SessionValuesSimulation extends Simulation {

    HttpProtocolBuilder httpProtocol =
            http
                    .baseUrl("https://postman-echo.com");

    ScenarioBuilder scn =
            scenario("GeMi_SessionValuesSimulation")
                    .exec(
                            http("GeMi_SessionValuesSimulation_first")
                                    .get("/status/414")
                                    .check(status().is(414).saveAs("GeMi_Response_Code"))
                    ).exec(session -> {
                        System.out.println("GeMi_Response_Code: " + session.get("GeMi_Response_Code").toString());
                        Session session1 = session.set("GeMi_Response_Code_1", session.get("GeMi_Response_Code").toString() + "_1");
                        return session1;
                    }).exec(session -> {
                        System.out.println("GeMi_Response_Code_1: " + session.get("GeMi_Response_Code_1").toString());
                        Session session2 = session.set("GeMi_Response_Code_2", session.get("GeMi_Response_Code_1").toString() + "_2");
                        return session2;
                    }).exec(session -> {
                        System.out.println("GeMi_Response_Code_2: " + session.get("GeMi_Response_Code_2").toString());
                        return session;
                    }).exec(
                            http("GeMi_SessionValuesSimulation_later")
                                    .get("/get?foo=#{GeMi_Response_Code_2}")
                                    .check(jmesPath("args.foo").is("414_1_2")));

    {
        setUp(scn.injectOpen(atOnceUsers(1)).protocols(httpProtocol));
    }
}

Maybe this will help :slight_smile: