4

I'm trying to upload multiple files.

val ktorVersion = "1.5.0"
val serializationVersion = "1.0.1"

That is how I'm doing that:

  override suspend fun uploadFiles(
    binaryFiles: Map<String,ByteArray>
  ): BaseResponse<List<String>> {
    return client.submitForm {

      url(fileUploadUrl)
      method = HttpMethod.Post
      body = MultiPartFormDataContent(
        formData {
          headers{
            append("Content-Type", "application/json")
            append("Authorization", "Bearer $token}")
          }
          binaryFiles.entries.forEach {
            append(
              key = "files",
              value = it.value,
              headers = Headers.build {
                append(HttpHeaders.ContentDisposition, "filename=${it.key}")
              }
            )
          }
        }
      )
    }
  }

But it throws exception

kotlinx.serialization.SerializationException: Serializer for class 'MultiPartFormDataContent' is not found.
Mark the class as @Serializable or provide the serializer explicitly.
    at kotlinx.serialization.internal.Platform_commonKt.serializerNotRegistered(Platform.common.kt:91)
    at kotlinx.serialization.SerializersKt__SerializersKt.serializer(Serializers.kt:130)
    at kotlinx.serialization.SerializersKt.serializer(Unknown Source)
    at io.ktor.client.features.json.serializer.KotlinxSerializerKt.buildSerializer(KotlinxSerializer.kt:82)
    at io.ktor.client.features.json.serializer.KotlinxSerializerKt.access$buildSerializer(KotlinxSerializer.kt:1)
    at io.ktor.client.features.json.serializer.KotlinxSerializer.writeContent$ktor_client_serialization(KotlinxSe
    at io.ktor.client.features.json.serializer.KotlinxSerializer.write(KotlinxSerializer.kt:26)
    at io.ktor.client.features.json.JsonFeature$Feature$install$1.invokeSuspend(JsonFeature.kt:150)
    at io.ktor.client.features.json.JsonFeature$Feature$install$1.invoke(JsonFeature.kt)
    at io.ktor.util.pipeline.SuspendFunctionGun.loop(SuspendFunctionGun.kt:243)
    at io.ktor.util.pipeline.SuspendFunctionGun.proceed(SuspendFunctionGun.kt:113)
    at io.ktor.util.pipeline.SuspendFunctionGun.proceedWith(SuspendFunctionGun.kt:123)
    at io.ktor.client.features.HttpCallValidator$Companion$install$1.invokeSuspend(HttpCallValidator.kt:106)
    at io.ktor.client.features.HttpCallValidator$Companion$install$1.invoke(HttpCallValidator.kt)
    at io.ktor.util.pipeline.SuspendFunctionGun.loop(SuspendFunctionGun.kt:243)
    at io.ktor.util.pipeline.SuspendFunctionGun.proceed(SuspendFunctionGun.kt:113)
    at io.ktor.client.features.HttpRequestLifecycle$Feature$install$1.invokeSuspend(HttpRequestLifecycle.kt:37)
    at io.ktor.client.features.HttpRequestLifecycle$Feature$install$1.invoke(HttpRequestLifecycle.kt)
    at io.ktor.util.pipeline.SuspendFunctionGun.loop(SuspendFunctionGun.kt:243)
    at io.ktor.util.pipeline.SuspendFunctionGun.proceed(SuspendFunctionGun.kt:113)
    at io.ktor.util.pipeline.SuspendFunctionGun.execute(SuspendFunctionGun.kt:133)
    at io.ktor.util.pipeline.Pipeline.execute(Pipeline.kt:79)
    at io.ktor.client.HttpClient.execute(HttpClient.kt:187)
    at io.ktor.client.statement.HttpStatement.executeUnsafe(HttpStatement.kt:104)
    at com.example.package.core.data.network.services.upload.FileUploadApiImpl.uploadFiles(FileUploadApi.kt:99)

I have also tried in that way but got same problem:

  suspend fun uploadFilesTest(
    binaryFiles: Map<String,ByteArray>
  ): BaseResponse<List<String>> {
    return client.post(fileUploadUrl) {
      headers {
        append("Content-Type", ContentType.Application.Json)
        append("Authorization", "Bearer $token")
      }
      body = MultiPartFormDataContent(
        formData {
          binaryFiles.entries.forEach {
            this.appendInput(
              key = "files",
              size = it.value.size.toLong(),
              headers = Headers.build {
                append(HttpHeaders.ContentDisposition, "filename=${it.key}")
              },
            ){ buildPacket { writeFully(it.value) }}
          }
        }
      )
    }
  }
Jemo Mgebrishvili
  • 5,187
  • 7
  • 38
  • 63

2 Answers2

6

Looking at your last snippet where you say you have the same problem, it seems like you are specifying content-type in headers like this

append("Content-Type", ContentType.Application.Json)

but you also want it to be multipart by setting body to be MultiPartFormDataContent which also needs to be defined as a header content-type:multipart/form-data; ... so it might be that this conflict is causing the issue.

I actually just tried it myself in my code and that seemed to be the issue for me.

user1071762
  • 625
  • 1
  • 9
  • 14
  • 3
    Removing the content type from headers fixed the problem – Jemo Mgebrishvili Mar 29 '21 at 11:54
  • Awesome removing the explicit header from my Multipart request did work. So I neither add `contetn/json` nor multipart in Ktor. All I do is the add header on `MultiPartFormDataContent` in the body – sud007 Mar 28 '22 at 14:22
  • Here is a gist of how I did this https://stackoverflow.com/a/71649114/1149398 – sud007 Apr 11 '22 at 14:03
0

I did this for Android but it may also work for ktor. At first, add this line to dependencies in the build project gradle:

classpath "org.jetbrains.kotlin:kotlin-serialization:1.5.21"

Add also these to the build app gradle:

plugins {
    ...
    id 'kotlinx-serialization'
}
dependencies {
    ...
    implementation 'org.jetbrains.kotlinx:kotlinx-serialization-json:1.2.2'
}
Saeed Ir
  • 1,974
  • 2
  • 20
  • 22