Created
June 21, 2020 20:50
-
-
Save Alqueraf/24040438e544b02becb23a8c7a9c93bd to your computer and use it in GitHub Desktop.
Ktor Multipart Request
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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