Issue
I am using Ktor 1.2.2 and I have an InputStream object that I want to use as the body for an HttpClient request I make down the line. Up until Ktor 0.95 there was this InputStreamContent object that seemed to do just that but it has been removed from Ktor at version 1.0.0 (couldn't figure out why unfortunately).
I can make it work using a ByteArrayContent (see code below) but I'd rather find a solution that does not require loading the entire InputStream into memory...
ByteArrayContent(input.readAllBytes())
This code is a simple test case that emulate what I'm trying to achieve:
val file = File("c:\\tmp\\foo.pdf")
val inputStream = file.inputStream()
val client = HttpClient(CIO)
client.call(url) {
method = HttpMethod.Post
body = inputStream // TODO: Make this work :(
}
// [... other code that uses the response below]
Let me know if I missed any relevant information,
Thanks!
Solution
The only API (that I have found...) in Ktor 1.2.2 is potentially sending a multi-part request, which would require your receiving server to be able to handle this, but it does support a direct InputStream.
From their docs:
val data: List<PartData> = formData {
// Can append: String, Number, ByteArray and Input.
append("hello", "world")
append("number", 10)
append("ba", byteArrayOf(1, 2, 3, 4))
append("input", inputStream.asInput())
// Allow to set headers to the part:
append("hello", "world", headersOf("X-My-Header" to "MyValue"))
}
This being said, I don't know how it works internally and likely still loads to memory the entirety of the stream.
The readBytes method is buffered, so wont take up the entirety of memory.
inputStream.readBytes()
inputStream.close()
As a note, you are still required to close the inputStream with most methods on InputStreams
Ktor Source: https://ktor.io/clients/http-client/call/requests.html#the-submitform-and-submitformwithbinarydata-methods
Kotlin Source: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.io/java.io.-input-stream/index.html
Answered By - Benjamin Charais
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.