Using custom java function

I am using gatling with java and maven.
My goal is to login to an application (HTTP request which is working fine)
after logging in I need to take ApiToken from the response (which is working fine)
now I need to manipulate the token by concatinating it with “$” and then encode it to base64 format.
using this encoded token I want to establish a websocket connection and continue the load test.

Problem : I am unable to do the encoding operation. I tried creating a regular java function under the class which extends simulation, it gives me error. is there a better way to resolve this? Maybe a code snippet can help!

thank you

Hello,

For example, you can do something like this (if you saved your ApiToken using a session and named it foo):

to be invoked in a function to make the session accessible. More info here

String originalInput = "$" + session.getString("foo");
String encodedString = Base64.getEncoder().encodeToString(originalInput.getBytes());

Also, can you provide a snippet of the error logs? :blush:


I’ve done it this way. Not sure why getBytes() is not working. Im working on java 22.
and imported below
import java.util.*;
import java.lang.String;
import java.lang.Object;

Because you’re using the generic get<T> instead of getString.

1 Like

Can you replace #{apitoken} with session.getString("apitoken") on line 37? Also, why do you create two new sessions?

Here are some examples here.

.exec(session ->  {
String value = "\u001"+"$"+ session.getString("apitoken");
String encodedUrl = Base64.getEncoder().encodeToString(value.getBytes());
Session newSession = session.set("token",encodedUrl);
return newSession;
})
1 Like

Thanks Samir! I understood slandelle.
the getByte() issue is fixed.
I cant access the app due to downtime now. But the next question is how do I access the encoded session token in the next exec block when I’m trying to establish the websocket connection?

Without the code, it will be complicated. You can check this or this.


I need to pass the encoded token grabbed earlier to the next exec block (starts line 49). so I am trying to pass it like #{token}. when I use session.getString(“token”), it cant find the session variable, so how do i pass the session to the next exec given I don’t have anything to return in this block.

What does your system.out.println display?

Also, can you use .connect(“/diffusion?#{token}”)?

Thanks Samir.
The token is printed correctly. The new error im seeing this premature closure of websocket.
it shows OK for open ws connection but not sure if it is actually opening the connection.

Hello,

Can you add this to your logback configuration

<logger name="io.gatling.http.action.ws.fsm" level="DEBUG" />

About the “Premature close” exception, it means that the connection socket between Gatling and your server was closed before reaching the end of the body.

This can come from multiples reason like:

  • An intermediate server (proxy) forcely close the connection at some time
  • Wrong value for Content-length header.
  • Server answers correctly with 200 and correct headers, but don’t let the connection ends to transmit the full body (or the body is corrupted)

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