okhttp icon indicating copy to clipboard operation
okhttp copied to clipboard

OkHttp retries consume RequestBody streams, causing empty payloads on retry for multipart uploads

Open Hawra2020 opened this issue 2 months ago • 3 comments

OkHttp version 4.12.0

Description

When retryOnConnectionFailure is enabled (default behavior), OkHttp retries failed requests (e.g., due to connection errors) by reusing the same RequestBody. For multipart uploads using a non-repeatable InputStream (e.g., ByteArrayInputStream), the stream is fully consumed during the first attempt. This results in an empty payload (0-byte file) being sent on retry, leading to data loss or incomplete uploads.

This issue affects scenarios like file uploads where the InputStream cannot be reliably reset (e.g., network streams). Workarounds include disabling retries (retryOnConnectionFailure(false)) but they are not ideal when relying on automatic retries.

A common attempt to fix this is calling InputStream.reset() after marking the stream, but this fails because OkHttp's retry mechanism does not provide a hook to reset the stream between attempts.

Steps to Reproduce

  1. Create an OkHttp client with retryOnConnectionFailure(true) (default).
  2. Prepare a multipart RequestBody using a non-repeatable InputStream (e.g., ByteArrayInputStream).
  3. Send a POST request to a mock server (e.g., via WireMock) that simulates a connection failure on the first attempt (e.g., returns an empty response or fault to trigger retry).
  4. Observe the retry request: The InputStream is exhausted, sending an empty body (0 bytes).

Hawra2020 avatar Oct 10 '25 12:10 Hawra2020

Are you returning true from isOneShot?

JakeWharton avatar Oct 21 '25 15:10 JakeWharton

I’m using a custom RequestBody that doesn’t override isOneShot(), so it currently returns the default (false).

Hawra2020 avatar Oct 21 '25 15:10 Hawra2020

You need to return true, and also upgrade to OkHttp 5.0 or newer where that flag is honored for multipart parts.

JakeWharton avatar Oct 21 '25 15:10 JakeWharton