Skip to content

Instantly share code, notes, and snippets.

@Alqueraf
Created June 21, 2020 20:50
Show Gist options
  • Save Alqueraf/24040438e544b02becb23a8c7a9c93bd to your computer and use it in GitHub Desktop.
Save Alqueraf/24040438e544b02becb23a8c7a9c93bd to your computer and use it in GitHub Desktop.
Ktor Multipart Request
suspend fun uploadFile(file: ByteArray, headerValue: String) = withContext(Dispatchers.IO) {
try {
// Prepare Input Stream
val inputStream = file.inputStream().asInput()
// Create Form Data
val parts: List<PartData> = formData {
// Optional Headers
append("headerKey", headerValue)
// File Input Stream
appendInput("file", size = file.size.toLong()) { inputStream }
}
// Launch Request
val response = multipartHttpClient.submitFormWithBinaryData<HttpResponse>("https://storage.example.org/upload", parts)
// Close Input Stream
inputStream.close()
} catch (t: Throwable) {
// Handle Error
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment