Gatling parameterization

I am using csv feeder data source to paramterize json request body.
csv file:
categoryId
BIRDS
FISH
DOGS
REPTILES
CATS

val csvFeeder = csv(“categories.csv”).random()

val scn = scenario("PetStoreSimulation")
    .exec(http("Homepage").get("/actions/Catalog.action"))
    .feed(csvFeeder)
    .exec(http("Catalog #{categoryId}")
        .post("/actions/Catalog")
       .body(ElFileBody("cat_request.json").asJson()
      .check(status.is(200))
        

below is cat_request json

{
  "Id": "#{categoryId}",
  "catalogType": "double",
}

Somehow I get 400 status code .
In most example I have seen ,data is passed in query or formParam of the request. Is there a way to actually pass csv test data to json payload file from the feeder?

400 means Bad Request, ie that your request is malformed.

Your JSON payload is indeed malformed because of a dandling comma.

{
“Id”: “#{categoryId}”,
“catalogType”: “double”
}
I made mistake in the json here in the post but nonetheless my json payload used for the request was valid. I assumed probably data from csv was not populated to the json keys. Because when I use static data for categoryId request is successful.
Does Gatling allow to pass paramters(test data ) from csv feeder to json referenced ELFileBody()? Because I need to pass test data record from feeder to request json placeholder variable #{categoryId} and this does not work.

Any chance you’re using an old version of Gatling? Have you tried to upgrade, as required before posting here?

So this is exactly your issue. You’re using the modern Gatling Expression Language syntax with an obsolete Gatling version (3.7.6). Please upgrade. The latest version as of now is 3.9.6.

Yeah thanks I will check that.

Different question, different topic.

Still encountered problem(400 status - malformed request) where csv data is to not passed to the parameters defined in request payload even after updating to gatling version 3.9.6. Below is snippet code of code . Don’t really know what is wrong.

HttpProtocolBuilder httpProtocol = http
                .baseUrl("https://jsonplaceholder.typicode.com")
                .acceptHeader("application/json")
                .contentTypeHeader("application/json");

 var feeder = csv("data/posts.csv");

        ScenarioBuilder loadScn = scenario("Create post")
                .feed(feeder)
                .exec(http("create new post request")
                        .post("/posts")
                        .body(ElFileBody("data/create-post.json")).asJson()
                        .check(status().is(200))
                        ).pause(1)

        setUp(
                loadScn.injectOpen(
                        nothingFor(2),
                          rampUsers(5).during(10)
                )
        ).protocols(httpProtocol)

posts.csv (test data source)

title,content,userId
foo,bar,1
echo,meow,2
todo,cleanup,3

create-post.json file (ElFileBody)

{
  "title": "#{title}",
  "body": "#{content}",
  "userId": "#{userId}"
}

Hi @Pof,

From your CSV, it seems that userId is a number. But you created a String in your create-post.json file.

Try removing the double quotes around it.

{
  "title": "#{title}",
  "body": "#{content}",
  "userId": #{userId}
}

Cheers!

Removing the quotes makes the json file invalid. IDE highlights that file as misconstructed.

Anyways I tried using theJsonFile feeder instead of csv feeder and it is able to pass test data to request payload file.
However Gatling crashes when virtual users does to match feeder records even though I am using the circular strategy .
rampUsersPerSec(1).to(5).during(10) against 4 records in json feeder
Error using circular strategy var feeder = jsonFile(“data/posts.json”).circular()
2023-12-02 14:44:03 ERROR main app.Gatling$ Run crashed
java.lang.reflect.InvocationTargetException: null
at jdk.internal.reflect.DirectConstructorHandleAccessor.newInstance(DirectConstructorHandleAccessor.java:79) ~[?:?]
at java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499) ~[?:?]
at java.lang.reflect.Constructor.newInstance(Constructor.java:483) ~[?:?]
at io.gatling.app.SimulationClass$Java.params(SimulationClass.scala:35) ~[gatling-app-3.9.5.jar:3.9.5]
at io.gatling.app.Runner.load(Runner.scala:72) ~[gatling-app-3.9.5.jar:3.9.5]
at io.gatling.app.Runner.run(Runner.scala:57) ~[gatling-app-3.9.5.jar:3.9.5]
at io.gatling.app.Gatling$.start(Gatling.scala:89) ~[gatling-app-3.9.5.jar:3.9.5]
at io.gatling.app.Gatling$.fromMap(Gatling.scala:43) ~[gatling-app-3.9.5.jar:3.9.5]

Error without using any feeder strategy var feeder = jsonFile(“data/posts.json”)
java.lang.IllegalStateException: Feeder json(post.json) is now empty, stopping engine
at io.gatling.core.action.FeedActor$$anonfun$receive$1.applyOrElse(FeedActor.scala:83) ~[gatling-core-3.9.5.jar:3.9.5]

Indeed, what I provided, ie

{
  "title": "#{title}",
  "body": "#{content}",
  "userId": #{userId}
}

is not a valid JSON content. It is a template to produce JSON
Keep in mind that gatling will replace data in this template.

For instance (with first value line):

{
  "title": "foo",
  "body": "bar",
  "userId": 1
}

That is a valid JSON

Cheers!