How to use dynamically computed object as request body

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 and how to ask a question topics.
I provided a SSCCE (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!

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()
  • if you’re using a function, you’re the one in charge of generating a String.

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.

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()
)

Please check the documentation of asJson() to understand what it really does. Gatling HTTP protocol reference - request

And then try jsonStringify() as suggested.

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

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()
)

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

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

Glad it worked :tada: