Dynamic File upload

Hey folks,

Thanks for building out this great tool!

I’m trying to upload a file using gatling.
I’m using multi-part requests for this purpose.

I want to be able to pre-process the file before I upload it (The file itself is a spreadsheet who’s data I intend to modify at runtime (using Apache POI) before uploading the file)

Checking the gatling documentation
I see there is one pre-processor for zipping the payload.
Gatling - HTTP Request.

In the below snippet, the ‘file’ argument to RawFileBodyPart’ expects the path to the file
Is it possible to generate this path at runtime ? How can I achieve this ?

  .bodyPart(
    RawFileBodyPart("file", "${templateExcelImportFile}")
      .contentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
      .fileName("${fileName}")
  )

Hi @SuperJuBoy,

Welcome back!

The filePath argument of the RawFileBodyPart is a Gatling Expression Language. So you can put a lambda (or a function in scala) there to be able to compute at runtime the String representing your wanted path.

  .bodyPart(
    RawFileBodyPart("file", session -> {
      String path = ...;
      return path;
    })
      .contentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
      .fileName("${fileName}")
  )

or in scala:

  .bodyPart(
    RawFileBodyPart("file", { session =>
      val path = ...
      path
      })
      .contentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
      .fileName("${fileName}")
  )

Cheers!

1 Like

Hey Thanks Sebastien for the prompt reply, I’ll try this out! just one last add-on query.

While writing/reading the file I don’t want to specify an absolute path on the filesystem. I’d like to specify some kind of relative path that would work irrespective of where I run my test.

For Feeders or when specifying request payloads from a file, the resources folder is where this data goes. Is there a directory where dynamically generated data can go ?

If I were to specify just the file name in the “path” variable in your example, would it still work ?

The path doesn’t have to be an absolute path. It can be the location of the resource in the classpath. Just like feeders.

1 Like

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