Skip to content

Instantly share code, notes, and snippets.

@raghunandankavi2010
Created February 4, 2021 10:28
Show Gist options
  • Save raghunandankavi2010/0a80d7f63ada60853e471f2e62004d27 to your computer and use it in GitHub Desktop.
Save raghunandankavi2010/0a80d7f63ada60853e471f2e62004d27 to your computer and use it in GitHub Desktop.
InputStreamRequestBody for retrofit
import okhttp3.MediaType
import okhttp3.RequestBody
import okio.BufferedSink
import okio.source
import java.io.IOException
import java.io.InputStream
class InputStreamRequestBody(private val contentType: MediaType, private val inputStream: InputStream) : RequestBody() {
override fun contentType(): MediaType {
return contentType
}
@Throws(IOException::class)
override fun contentLength(): Long {
return -1
}
@Throws(IOException::class)
override fun writeTo(sink: BufferedSink) {
sink.writeAll(inputStream.source())
}
}
@raghunandankavi2010
Copy link
Author

Usage val file = InputStreamRequestBody("multipart/form-data".toMediaType(),inputStream)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment