Help passing a saved session variable to a Java method

Hi all, I’m having a hard time passing a variable from my GET call to my DELETE call for some reason. I’ve done this a million times but in this case it does not work for me. Any help would be appreciated!! :smiley:

    public static final ChainBuilder getListOfStuff =
            exec(http("Get List of Stuff")
                    .get("/example/api/v1/stuffs")
                    .headers(getSessionHeaders())
                    .check(status().is(200))
                    .check(jsonPath("$..id").exists().saveAs("stuffsId"))
            );

    public static final ChainBuilder deleteStuffIfCurrentUserIsOwnerOfStuff;
    static {
        try {
            deleteStuffIfCurrentUserIsOwnerOfStuff =
                    exec(http("Delete Stuff If Current User Is Owner")
                            .delete("/example/api/v1/stuffs")
                            .headers(getStuffSessionHeaders("#{stuffsId}"))
                            .check(status().is(204))
                    );
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static Map<String, String> getStuffSessionHeaders(String stuffsId) throws IOException {
        System.out.println("stuffsId=========" + stuffsId);
...................
}

The result I get from trying to pass #{stuffsId} to a java method is
“stuffId=========#{stuffsId}” instead of the actual stuff ID i’m saving in GET from previous call.

Any help would be appreciated!

Thanks

Hi @gkandale,

Your issue lie in the fact that headers take a Map<String, String> as parameter and doesn’t involve Gatling Expression Language.

For the particular header you want with value from session (by expression language, for instance), you should use the header (singular) method.

Hope that helps,

Cheers!

Hey thank you Sébastien for your super fast response! Thats awesome!!
Can you give me an example? I don’t completely understand :frowning:

Thanks again!!
George

This scenario will sent the value #{hello} without replacing it in the header.

  exec(session -> session.set("hello", "world"))
  .exec(http("Get List of Stuff")
          .get("/api/endpoint")
          .headers(Map.of("X-Hello", "#{hello}"))
      );

But the following one will set the X-Hello header to the right value:

  exec(session -> session.set("hello", "world"))
  .exec(http("Get List of Stuff")
          .get("/api/endpoint")
          .header("X-Hello", "#{hello}")
      );

Does that help?

Cheers!

Hey Sebastian,

I’m trying to pass a variable to a method call which builds out the headers, including calculating the bearer token, but the stuff ID i’m passing to the method does not send the value, it sends “#{stuffId}” as the actual string

    public static final ChainBuilder deleteStuffIfCurrentUserIsOwnerOfStuff;
    static {
        try {
            deleteStuffIfCurrentUserIsOwnerOfStuff =
                    exec(http("Delete Stuff If Current User Is Owner")
                            .delete("/example/api/v1/stuffs")
                            .headers(getStuffSessionHeaders("#{stuffsId}"))
                            .check(status().is(204))
                    );
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static Map<String, String> getStuffSessionHeaders(String stuffsId) throws IOException {
        System.out.println("stuffsId=========" + stuffsId);
        
        STUFF_HELPER = new STUFFHelper(context);
        String stuffAccessToken = STUFF_HELPER.getSwitchStuffToken(stuffsId, getUserTokens());

        Map<String, String> stuffsSessionHeaders = new HashMap<String, String>();
        stuffsSessionHeaders.put("content-Type", "application/json");
        stuffsIdSessionHeaders.put("accept", "application/json");
        stuffsIdSessionHeaders.put("authorization", "Bearer " + accountAccessToken);
        return accountSessionHeaders;
    }

As stated in the first alert box of the Concepts page:

The components of this DSL are mere definitions of the desired effect. They don’t generate the desired effect where you invoke them in your own code. Only when chained with other components so they are ultimately passed to the setUp, can the Gatling engine interpret them and produce the desired effect.

So, the call to your getStuffSessionHeaders is executed during the initialization phase. Way before the first virtual user is created.

Cheers!

I’m trying to change tokens mid test - user needs a space specific token after they sign up to become an owner of some space. I need to get a new token for the user to be able to work within their specific space (including deleting their space after they are done with it. Any thought on how I can do this within gatling?

Figured it out…wrote a gatling component instead of reusing the automation code that I’m trying to integrate with :frowning:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.