MalformedInputException: Input length = 1, when uploading an image multipart
See original GitHub issueKtor Version and Engine Used (client or server and name) Ktor 1.3.2, Kotlin 1.3.70, Netty server, ContentNegotiation, Jackson parser, Exposed.
I simply used the Ktor project generator for Gradle, and checked a few boxes for modules as mentioned above. After which I managed to connect a database & a few endpoints, but my core endpoint for uploading images doesn’t work.
Describe the bug When trying to upload a simple image using Retrofit & Multipart support in Android, I get a MalformedInputException on my server.
Retrofit/Android code:
@Multipart
@POST("/api/upload")
suspend fun uploadImages(@Part("image") imageFile: RequestBody): UploadResponse
Upload code:
val requestBody: RequestBody = MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("title", "Image Upload")
.addFormDataPart(
"image", // part name
file.path.split("/").last(), // file name
file.asRequestBody("image/jpg".toMediaType()) // Content-Type: image/jpg
)
.build()
return apiService.uploadImages(requestBody)
Using the new way of creating RequestBody objects in OkHttp. I followed the documentation to receive the multipart data, and I’ve formed a MalformedInputException.
To Reproduce Steps to reproduce the behavior:
- Use Retrofit to Upload an image to a Ktor server.
- call
receiveMultipart()
to receive MP on the Ktor side. - See error
Expected behavior Multipart data should be received correctly.
Stack trace:
[nioEventLoopGroup-4-1] ERROR Application - Unhandled: POST - /api/upload
io.ktor.utils.io.charsets.MalformedInputException: Input length = 1
at io.ktor.utils.io.charsets.CharsetJVMKt.throwExceptionWrapped(CharsetJVM.kt:323)
at io.ktor.utils.io.charsets.CharsetJVMKt.decodeImplSlow(CharsetJVM.kt:289)
at io.ktor.utils.io.charsets.CharsetJVMKt.decodeExactBytes(CharsetJVM.kt:254)
at io.ktor.utils.io.core.StringsKt.readTextExactBytes(Strings.kt:293)
at io.ktor.utils.io.core.StringsKt.readTextExactBytes$default(Strings.kt:292)
at io.ktor.utils.io.core.AbstractInput.readText(AbstractInput.kt:468)
at io.ktor.utils.io.core.AbstractInput.readText$default(AbstractInput.kt:465)
at io.ktor.http.cio.CIOMultipartDataBase.partToData(CIOMultipartData.kt:127)
at io.ktor.http.cio.CIOMultipartDataBase$partToData$1.invokeSuspend(CIOMultipartData.kt)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:56)
at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:164)
at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:472)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:500)
at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989)
at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
at java.base/java.lang.Thread.run(Thread.java:844)
Issue Analytics
- State:
- Created 3 years ago
- Reactions:6
- Comments:8 (3 by maintainers)
Top GitHub Comments
Same for me with different stack trace (logging feature). In my case disabling the logging feature is a suitable workaround.
In the course of researching the problem, I saw that if the boundary body does not contain the name of the
filename
, then this error occurs. after adding unique filename to boundary body the problem went away