# How to use dynamically computed object as request body

**URL:** https://community.gatling.io/t/how-to-use-dynamically-computed-object-as-request-body/9699
**Category:** Gatling (Open-Source)
**Created:** [June 17, 2025, 12:27am UTC](https://community.gatling.io/t/how-to-use-dynamically-computed-object-as-request-body/9699 "2025-06-17T00:27:07Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Terence](https://avatars.discourse-cdn.com/v4/letter/t/aca169/32.png) [@Terence](https://community.gatling.io/u/Terence)
#### Post date: [June 17, 2025, 12:27am UTC](https://community.gatling.io/t/how-to-use-dynamically-computed-object-as-request-body/9699/1 "2025-06-17T00:27:07Z")

</div>

**Gatling version** :  
**Gatling flavor** : java kotlin scala javascript typescript  
**Gatling build tool** : maven gradle sbt bundle npm

I made sure I’ve update my Gatling version to the latest release  
 I read the [guidelines](https://community.gatling.io/t/welcome-in-the-gatling-community-our-code-of-conduct/6462) and [how to ask a question](https://community.gatling.io/t/how-to-ask-a-question/6538) topics.  
 I provided a [SSCCE](http://www.sscce.org/) (or at least, all information to help the community understand my topic)  
 I copied output I observe, and explain what I think should be.

Hi, All,

I want to pass an object to request body. I know we have RawFileBody and ElFileBody which can store the body in an json file, and ElFileBody can have EL to input dynamic values. But now I have a request body which is from a response body form a previous request and I need to do some modification to this body, then pass it to the next request. I have the following two questions:

1. Do we or will we have any request body method that can just pass an object into it, not the body file path.
2. If we don’t and won’t have this method, how to pass this dynamic body, through StringBody? I tried to save that object in a session, then use `StringBody("#{savedObjectBody}")`, but the request body is StringChunksRequestBody, not the json body I need.

Thanks!

---

<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 17, 2025, 6:36am UTC](https://community.gatling.io/t/how-to-use-dynamically-computed-object-as-request-body/9699/2 "2025-06-17T06:36:40Z")

</div>

Hi,

Not sure what you are doing, I’d need you show some code to get it right.  
What I can say is that objects don’t get magically turned into a String.  
I guess you’re trying to serialize a JSON object into a String.  
Then,

- if you’re using Gatling Expression Language, you’re looking for [`jsonStringify()`](https://docs.gatling.io/concepts/session/el/#built-in-functions)
- if you’re using a function, you’re the one in charge of generating a String.

---

<div class="post-metadata">

### Author: ![Terence](https://avatars.discourse-cdn.com/v4/letter/t/aca169/32.png) [@Terence](https://community.gatling.io/u/Terence)
#### Post date: [June 17, 2025, 7:26am UTC](https://community.gatling.io/t/how-to-use-dynamically-computed-object-as-request-body/9699/3 "2025-06-17T07:26:22Z")

</div>

Hi, I have the following example, so all the requests’ response and payload are json body. So I saved the request 1’s response body which will be modified a bit and used as the request body for request 2. I need the request 2’s request body to be json format, and I put `asJson()` after the `body`, but in the real request, it’s still sending StringChunksRequestBody which are not accepted by the backend.

```auto
exec(
      http("request 1")
            .get("/request1")
            .check(bodyString().saveAs("object"))，
).exec(session) => {
  const object= JSON.parse(session.get("object"))
  const updateObject = {...object, { add: "something" }
  const newSession = session.set("object", updateObject);
}).exec(
      http("request 2")
          .post("/request2")
          .body(StringBody("#{object}")
          .asJson()
)

```

---

<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 17, 2025, 7:40am UTC](https://community.gatling.io/t/how-to-use-dynamically-computed-object-as-request-body/9699/4 "2025-06-17T07:40:33Z")

</div>

Please check the documentation of `asJson()` to understand what it really does. [Gatling HTTP protocol reference - request](https://docs.gatling.io/reference/script/http/request/#asxxx)

And then try `jsonStringify()` as suggested.

---

<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 17, 2025, 8:04am UTC](https://community.gatling.io/t/how-to-use-dynamically-computed-object-as-request-body/9699/5 "2025-06-17T08:04:06Z")

</div>

I guess you can even directly do it in a function.

```javascript
exec(
      http("request 1")
            .get("/request1")
            .check(bodyString().saveAs("object")),
http("request 2")
          .post("/request2")
          .body(StringBody((session) => {
            const object= JSON.parse(session.get("object"));
            const updateObject = {...object, { add: "something" }
            return JSON.stringify(updateObject);
})
          .asJson()
)

```

---

<div class="post-metadata">

### Author: ![Terence](https://avatars.discourse-cdn.com/v4/letter/t/aca169/32.png) [@Terence](https://community.gatling.io/u/Terence)
#### Post date: [June 17, 2025, 10:10am UTC](https://community.gatling.io/t/how-to-use-dynamically-computed-object-as-request-body/9699/6 "2025-06-17T10:10:29Z")

</div>

Awesome, it worked, I thought expression language is returning a string, so didn’t think of JSON.stringfy it.

---

<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 17, 2025, 5:26pm UTC](https://community.gatling.io/t/how-to-use-dynamically-computed-object-as-request-body/9699/7 "2025-06-17T17:26:51Z")

</div>

Your function was returning an object, so we were just calling `toString` on it, which doesn’t produce JSON.

Glad it worked 🎉

---

<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 17, 2025, 5:27pm UTC](https://community.gatling.io/t/how-to-use-dynamically-computed-object-as-request-body/9699/8 "2025-07-17T17:27:49Z")

</div>

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