---
title: Multipart/form-data Upload
slug: smart-care/multipartform-data-upload
docTags: 
createdAt: 2025-07-09T08:22:23.145Z
---



| **Information** |                                                                                                              |
| --------------- | ------------------------------------------------------------------------------------------------------------ |
| Description     | Code snippet for fetching a pre-signed upload URL and uploading ZIP file using multipart/form-data.          |
| Purpose         | The function:<br />* Builds the pre-signed upload POST request
* Uploads the ZIP file as multipart/form-data |
| Language        | Kotlin                                                                                                       |

### Input Parameters

- zipFile: `File`
  &#x20;The ZIP file containing the JPG images.
- uploadUrl: `String`&#x20;
  &#x20;The base S3 endpoint URL provided by the `/request-url` API endpoint. It is typically in the format of *https\://\<bucket-name>.s3.amazonaws.com/*.
- fields: `Map<String, String>`&#x20;
  &#x20;This is a map of key-value pairs that must be included in the multipart request's form data.

### Output

- success: `Boolean`&#x20;
  &#x20;Value is `true` when the upload succeeded.

### Snippet

```kotlin
import okhttp3.*
import java.io.File

fun uploadZipToPresignedUrl(zipFile: File, uploadUrl: String, fields: Map<String, String>): Boolean {
    val client = OkHttpClient()

    val multipartBodyBuilder = MultipartBody.Builder().setType(MultipartBody.FORM)

    // Add all required AWS fields
    for ((key, value) in fields) {
        multipartBodyBuilder.addFormDataPart(key, value)
    }

    // Add file as "file" field
    multipartBodyBuilder.addFormDataPart(
        "file", zipFile.name,
        RequestBody.create(MediaType.parse("application/zip"), zipFile)
    )

    val request = Request.Builder()
        .url(uploadUrl)
        .post(multipartBodyBuilder.build())
        .build()

    client.newCall(request).execute().use { response ->
        if (!response.isSuccessful) {
            println("Upload failed with code: ${response.code}")
            return false
        }

        return true
    }
}

```

### Notes

- The above code snippet was **AI-generated** and not yet tested on syntax or runtime errors. It merely serves as an example implementation. Be **cautious** when using this snippet.
- It's important to add the AWS fields first and add the "file" key as latest.

