Ktor MultiPartFormDataContent request

Hi I have formdata request with OkHttpClient and it is working I want to use it in Ktor but I am getting error when I use Ktor. What am I doing wrong in Ktor?

OkHttpClient (Working)

val client = OkHttpClient.Builder()
val body = MultipartBody.Builder().setType(MultipartBody.FORM)
  .addFormDataPart("choice", null,
 "John Wick".toRequestBody("application/json".toMediaType()))
    .addFormDataPart("image",path,
        File(path).asRequestBody("application/octet-stream".toMediaType()))
    .build()
val request = Request.Builder()
    .url("https://example.com")
    .post(body)
    .addHeader("Authorization", "Bearer key")
    .build()

Ktor(Not working)

val httpResponse = client.submitForm {
      url("https://example.com")
      method = HttpMethod.Post
      contentType(ContentType.MultiPart.FormData)
      header("Authorization", "Bearer key")
      body = MultiPartFormDataContent (
          formData {
              append("choice","John Wick", Headers.build {
                  append(HttpHeaders.ContentType, ContentType.Application.Json)
              })
              append("image",path,Headers.build {
                  append(HttpHeaders.ContentType,  ContentType.Application.OctetStream)
              })
          }
      )
  }

  • What exactly isn’t working?

    – 

Leave a Comment