With Retrofit, how do you report upload progress for a multipart photo upload, and why might the progress bar fill twice?
answer
- Retrofit has no progress callback
- count bytes as they are written
- a sink that forwards and counts
- writeTo is not called just once
basics
~20 sWrap the photo's RequestBody in one whose writeTo counts bytes through an Okio ForwardingSink against contentLength(). writeTo can run more than once, for body-level logging or an OkHttp retransmission, so progress must restart on each pass.
solid answer
~50 sRetrofit has no progress API; progress is how many bytes OkHttp has written of the request body. I wrap the file's `RequestBody` before `createFormData`: the wrapper delegates `contentType()` and `contentLength()`, and in `writeTo()` wraps the sink in an Okio `ForwardingSink` whose `write()` adds `byteCount` to a counter local to that call, then calls `delegate.writeTo()` and flushes. `writeTo()` runs on the thread executing the call, an OkHttp dispatcher thread for `enqueue` and suspend methods, so I publish into a `StateFlow` and throttle. The bar fills twice because `writeTo()` can run more than once: `HttpLoggingInterceptor` at `Level.BODY` writes the whole body into a memory buffer to log it, and OkHttp resends a body after a stale connection or a 401 its `Authenticator` answers. Turning body logging off for uploads fixes the first; `isOneShot()` returning `true` stops both, at the cost of those automatic retries.
code
kotlin · 44 linesimport java.io.File
import kotlinx.coroutines.flow.MutableStateFlow
import okhttp3.MediaType
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.MultipartBody
import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.asRequestBody
import okio.Buffer
import okio.BufferedSink
import okio.ForwardingSink
import okio.buffer
class ProgressRequestBody(
private val delegate: RequestBody,
private val onProgress: (written: Long, total: Long) -> Unit,
) : RequestBody() {
override fun contentType(): MediaType? = delegate.contentType()
override fun contentLength(): Long = delegate.contentLength()
override fun writeTo(sink: BufferedSink) {
val total = contentLength()
var written = 0L // per pass: a second writeTo() starts again from zero
val counting = object : ForwardingSink(sink) {
override fun write(source: Buffer, byteCount: Long) {
super.write(source, byteCount)
written += byteCount
onProgress(written, total)
}
}.buffer()
delegate.writeTo(counting)
counting.flush() // never close: the caller owns the sink
}
}
fun photoPart(file: File, percent: MutableStateFlow<Int>): MultipartBody.Part {
val body = ProgressRequestBody(file.asRequestBody("image/jpeg".toMediaType())) { written, total ->
if (total > 0) {
val next = (written * 100 / total).toInt()
if (next != percent.value) percent.value = next // safe from OkHttp's thread
}
}
return MultipartBody.Part.createFormData("photo", file.name, body)
}go deeper
Recall that Retrofit has no progress callback: progress comes from wrapping the file's RequestBody and counting the bytes its writeTo() passes to the sink.
Explain the decorator: delegate contentType and contentLength, count in a ForwardingSink's write, flush without closing, and publish from OkHttp's thread through a thread-safe holder.
Diagnose a bar that fills twice: body-level logging writes the body once into memory, and OkHttp retransmits after stale connections or authenticator follow-ups; pick logging changes or isOneShot knowingly.
Weigh isOneShot on upload bodies as a client-wide default: predictable single writes and no body logging against losing OkHttp's transparent retries on stale connections and token refreshes.
## Where upload progress comes from Retrofit builds requests and hands them to OkHttp; it has no progress callback of its own. Upload progress is therefore a question about the **request body**: how many of its bytes OkHttp has written to the connection so far. Every OkHttp `RequestBody` has two methods that matter here: - `contentLength()` returns the number of bytes `writeTo()` will write, or `-1` when that is unknown. - `writeTo(sink: BufferedSink)` writes the bytes. It must not close the sink. A progress-reporting body is a **decorator**: it delegates both methods to the real body and counts the bytes that pass through on their way to the sink. OkHttp's own recipes show the same pattern as an upload-progress sample. ## A counting wrapper ```kotlin class ProgressRequestBody( private val delegate: RequestBody, private val onProgress: (written: Long, total: Long) -> Unit, ) : RequestBody() { override fun contentType() = delegate.contentType() override fun contentLength() = delegate.contentLength() override fun writeTo(sink: BufferedSink) { val total = contentLength() var written = 0L val counting = object : ForwardingSink(sink) { override fun write(source: Buffer, byteCount: Long) { super.write(source, byteCount) written += byteCount onProgress(written, total) } }.buffer() delegate.writeTo(counting) counting.flush() } } ``` - The counter lives **inside** `writeTo()`, so every pass starts from zero instead of adding to the last one. - `flush()`, not `close()`: the bytes still buffered must reach the real sink, but closing it would break OkHttp's contract. - `total` may be `-1`; the UI should then show an indeterminate bar rather than divide by it. ## Wrap the file, not the whole request With Retrofit you never hold the final `MultipartBody`, because Retrofit assembles it from the `@Part` arguments. The practical place for the wrapper is the file's own body, before it becomes a part: `createFormData("photo", file.name, ProgressRequestBody(file.asRequestBody(type), onProgress))`. The total is then the file's length; the boundaries, part headers and caption are not counted, which costs a few hundred bytes of accuracy and nothing else. ## Which thread reports progress `writeTo()` runs on whichever thread is writing the request: | how the call runs | thread that calls writeTo() | |---|---| | `Call.execute()` | the calling thread | | `Call.enqueue()` | an OkHttp dispatcher thread | | a `suspend` service method | an OkHttp dispatcher thread, because Retrofit's suspend support is built on `enqueue` | So the callback must not touch views. Writing to a `MutableStateFlow` is safe from any thread, and the UI collects it on the main thread. The callback fires for every chunk written, often thousands of times for a large photo, so emit only when the whole percentage changes. ## Why the bar can fill twice The counter resets on each pass, so a second pass shows up as the bar running from zero again. Two things cause a second pass: 1. **Body-level logging.** `HttpLoggingInterceptor` at `Level.BODY` calls `writeTo()` into an in-memory buffer to log the body, then the request goes to the network and `writeTo()` runs again. The first pass also reads the whole photo into the heap. 2. **OkHttp retransmission.** OkHttp resends a request body after a failure on a reused connection the server had closed, and on some follow-ups: a 408, a 401 or 407 that the `Authenticator` answers, a 503 with `Retry-After: 0`, a 421. A retry layer above Retrofit that calls the method again also produces a new pass; that one is expected and visible to the user anyway. ## Choosing a fix - Keep `Level.BODY` off for upload calls: use `Level.HEADERS`, or no logging in release builds. Which interceptors a client carries is the lesson on composing Retrofit's OkHttp client. - Treat a restart as "retrying" in the UI rather than as a bug; the per-pass counter already makes it look right. - Override `isOneShot()` to return `true` on the wrapper when a second write must never happen. Because a `MultipartBody` is one-shot if any part is, the whole request then is: the logging interceptor omits the body, and OkHttp will not resend it. The cost is that a stale-connection failure or a 401 the `Authenticator` could have fixed now reaches the caller instead of being retried. ## Completion and cancellation Reaching 100 % means the bytes left the client, not that the upload succeeded; the server can still reject the photo, so the success state comes from the response. Cancelling the coroutine cancels the Retrofit call and its OkHttp call, the write stops with an `IOException`, and the bar should reset rather than freeze at its last value.
- Why is the counter declared inside writeTo() rather than as a field of the wrapper?OkHttp can call `writeTo()` more than once for the same body: once into a buffer for body-level logging, again after a retransmission. A field would keep the first pass's total, so the bar would pass 100 %. A local counter makes each pass start from zero, which reads as a restart rather than a nonsense number.
- What does overriding isOneShot() to return true on the progress wrapper trade away?OkHttp stops resending the body: after a failure on a stale pooled connection, or a 401 its `Authenticator` answers, the caller now gets the error or the 401 instead of a transparent retry. In exchange, `HttpLoggingInterceptor` omits the body and progress can never run twice. Because a `MultipartBody` is one-shot when any part is, one flagged part covers the whole request.
- Retrofit has an onProgress callback you register on the service method.
- Count progress in contentLength(), since OkHttp calls it for every chunk.
- Update the progress view directly from inside writeTo().
- writeTo() is called exactly once per request.
- Close the sink at the end of writeTo() so the last bytes are sent.
- Progress reaching 100 % means the server accepted the upload.