# Using custom java function

**URL:** <https://community.gatling.io/t/using-custom-java-function/9049>\
**Category:** Gatling (Open-Source)\
**Tags:** gatling-academy\
**Created:** [June 5, 2024, 6:21pm UTC](https://community.gatling.io/t/using-custom-java-function/9049 "2024-06-05T18:21:49Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![banerjeeikshit](https://avatars.discourse-cdn.com/v4/letter/b/57b2e6/32.png) [@banerjeeikshit](https://community.gatling.io/u/banerjeeikshit)\
**Post date:** [June 5, 2024, 6:21pm UTC](https://community.gatling.io/t/using-custom-java-function/9049/1 "2024-06-05T18:21:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Samir](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/samir/32/1546_2.png) [@Samir](https://community.gatling.io/u/Samir)\
**Post date:** [June 5, 2024, 7:07pm UTC](https://community.gatling.io/t/using-custom-java-function/9049/2 "2024-06-05T19:07:56Z")

</div>

Hello,

For example, you can do something like this (if you saved your ApiToken using a [session](https://docs.gatling.io/reference/script/core/session/api/#session-api) and named it `foo`):

> to be invoked in a function to make the session accessible. More info [here](https://docs.gatling.io/reference/script/core/session/el/)

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

```

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

---

<div class="post-metadata">

**Author:** ![banerjeeikshit](https://avatars.discourse-cdn.com/v4/letter/b/57b2e6/32.png) [@banerjeeikshit](https://community.gatling.io/u/banerjeeikshit)\
**Post date:** [June 5, 2024, 7:49pm UTC](https://community.gatling.io/t/using-custom-java-function/9049/3 "2024-06-05T19:49:13Z")

</div>

> [@Samir](#):
>
> `getBytes()`

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/gatling2/original/2X/e/e77daa06057be3de7703926f829d766331a7775b.png)  
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;

---

<div class="post-metadata">

**Author:** ![slandelle](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/slandelle/32/4_2.png) [@slandelle](https://community.gatling.io/u/slandelle)\
**Post date:** [June 5, 2024, 7:51pm UTC](https://community.gatling.io/t/using-custom-java-function/9049/4 "2024-06-05T19:51:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Samir](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/samir/32/1546_2.png) [@Samir](https://community.gatling.io/u/Samir)\
**Post date:** [June 5, 2024, 8:00pm UTC](https://community.gatling.io/t/using-custom-java-function/9049/5 "2024-06-05T20:00:51Z")

</div>

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

Here are some examples [here](https://docs.gatling.io/reference/script/core/session/api/#setting-attributes).

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

```

---

<div class="post-metadata">

**Author:** ![banerjeeikshit](https://avatars.discourse-cdn.com/v4/letter/b/57b2e6/32.png) [@banerjeeikshit](https://community.gatling.io/u/banerjeeikshit)\
**Post date:** [June 5, 2024, 9:15pm UTC](https://community.gatling.io/t/using-custom-java-function/9049/7 "2024-06-05T21:15:16Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Samir](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/samir/32/1546_2.png) [@Samir](https://community.gatling.io/u/Samir)\
**Post date:** [June 5, 2024, 9:38pm UTC](https://community.gatling.io/t/using-custom-java-function/9049/8 "2024-06-05T21:38:59Z")

</div>

Without the code, it will be complicated. You can check [this](https://docs.gatling.io/reference/script/core/session/functions/#syntax) or [this](https://docs.gatling.io/reference/script/core/session/el/).

---

<div class="post-metadata">

**Author:** ![banerjeeikshit](https://avatars.discourse-cdn.com/v4/letter/b/57b2e6/32.png) [@banerjeeikshit](https://community.gatling.io/u/banerjeeikshit)\
**Post date:** [June 5, 2024, 10:16pm UTC](https://community.gatling.io/t/using-custom-java-function/9049/9 "2024-06-05T22:16:26Z")

</div>

![image](https://europe1.discourse-cdn.com/flex013/uploads/gatling2/original/2X/b/b31402dae80101743c1fa13e12c053da9e7d530a.png)  
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.

---

<div class="post-metadata">

**Author:** ![Samir](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/samir/32/1546_2.png) [@Samir](https://community.gatling.io/u/Samir)\
**Post date:** [June 5, 2024, 10:50pm UTC](https://community.gatling.io/t/using-custom-java-function/9049/10 "2024-06-05T22:50:48Z")

</div>

What does your system.out.println display?

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

---

<div class="post-metadata">

**Author:** ![banerjeeikshit](https://avatars.discourse-cdn.com/v4/letter/b/57b2e6/32.png) [@banerjeeikshit](https://community.gatling.io/u/banerjeeikshit)\
**Post date:** [June 6, 2024, 8:50am UTC](https://community.gatling.io/t/using-custom-java-function/9049/11 "2024-06-06T08:50:59Z")

</div>

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.

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/gatling2/original/2X/0/02c36c4fe6716c96a7287801e5322e30356825c5.png)

---

<div class="post-metadata">

**Author:** ![Samir](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/samir/32/1546_2.png) [@Samir](https://community.gatling.io/u/Samir)\
**Post date:** [June 6, 2024, 12:18pm UTC](https://community.gatling.io/t/using-custom-java-function/9049/12 "2024-06-06T12:18:55Z")

</div>

Hello,

Can you add this to your logback configuration

```auto
<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)

---

<div class="post-metadata">

**Author:** ![system](https://europe1.discourse-cdn.com/flex013/uploads/gatling2/original/2X/b/b148e0742de35c6280726cabf3ecd3de2d320f63.png) [@system](https://community.gatling.io/u/system)\
**Post date:** [July 6, 2024, 12:18pm UTC](https://community.gatling.io/t/using-custom-java-function/9049/13 "2024-07-06T12:18:59Z")

</div>

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