# Dynamic File upload

**URL:** <https://community.gatling.io/t/dynamic-file-upload/8247>\
**Category:** Gatling (Open-Source)\
**Created:** [August 28, 2023, 9:04am UTC](https://community.gatling.io/t/dynamic-file-upload/8247 "2023-08-28T09:04:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![SuperJuBoy](https://avatars.discourse-cdn.com/v4/letter/s/58956e/32.png) [@SuperJuBoy](https://community.gatling.io/u/SuperJuBoy)\
**Post date:** [August 28, 2023, 9:04am UTC](https://community.gatling.io/t/dynamic-file-upload/8247/1 "2023-08-28T09:04:27Z")

</div>

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.

> **[HTTP Request](https://gatling.io/docs/gatling/reference/current/http/request/#multipart)**
>
> Create HTTP requests

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](https://gatling.io/docs/gatling/reference/current/http/request/#processrequestbody).

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}")
  )

```

---

<div class="post-metadata">

**Author:** ![sbrevet](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/sbrevet/32/830_2.png) [@sbrevet](https://community.gatling.io/u/sbrevet)\
**Post date:** [August 28, 2023, 10:40am UTC](https://community.gatling.io/t/dynamic-file-upload/8247/2 "2023-08-28T10:40:51Z")

</div>

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.

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

```

or in scala:

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

```

Cheers!

---

<div class="post-metadata">

**Author:** ![SuperJuBoy](https://avatars.discourse-cdn.com/v4/letter/s/58956e/32.png) [@SuperJuBoy](https://community.gatling.io/u/SuperJuBoy)\
**Post date:** [August 28, 2023, 11:56am UTC](https://community.gatling.io/t/dynamic-file-upload/8247/3 "2023-08-28T11:56:30Z")

</div>

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 ?

> **[HTTP Request](https://gatling.io/docs/gatling/reference/current/http/request/?#full-body)**
>
> Create HTTP requests

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

---

<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:** [August 28, 2023, 11:58am UTC](https://community.gatling.io/t/dynamic-file-upload/8247/4 "2023-08-28T11:58:18Z")

</div>

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

---

<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:** [September 27, 2023, 11:58am UTC](https://community.gatling.io/t/dynamic-file-upload/8247/5 "2023-09-27T11:58:24Z")

</div>

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