From 4d935214f6ca7a18e3a35644c6b109faac5263e5 Mon Sep 17 00:00:00 2001 From: Hawra2020 Date: Fri, 10 Oct 2025 14:05:32 +0200 Subject: [PATCH 1/2] docs(boxsdk): fix 0 byte uploads being created on retry after application fails initla upload attempt --- docs/sdk/configuration.md | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/docs/sdk/configuration.md b/docs/sdk/configuration.md index 030b9d171..d38030674 100644 --- a/docs/sdk/configuration.md +++ b/docs/sdk/configuration.md @@ -326,4 +326,31 @@ public class BoxAPIConnectionWithTelemetry extends BoxAPIConnection { ``` Please note that you should not modify either `httpClient` or `request` parameters in the createNewCall method. -Altering these parameters can discard the BoxAPIConnection configuration and lead to unexpected behavior. \ No newline at end of file +Altering these parameters can discard the BoxAPIConnection configuration and lead to unexpected behavior. + +# OkHttp Default Retry Behavior + +When using the Box Java SDK, HTTP requests are made through an underlying OkHttp client. By default, OkHttp enables automatic retries for certain types of connection failures (e.g. network errors) via the `retryOnConnectionFailure(true)` setting. While this can improve reliability for transient issues, this can cause issues during file uploads with `BoxFolder.uploadFile(InputStream, String)`, particularly when the provided `InputStream` is consumed during a failed request and reused in a retry, potentially resulting in a 0-byte file being uploaded. + +To leverage the Box SDK’s retry strategy and avoid issues with OkHttp’s default retries, you can disable OkHttp’s automatic retries by customizing the BoxAPIConnection. +To disable OkHttp’s automatic retries, create a custom BoxAPIConnection subclass and override the `modifyHttpClientBuilder` method to set `retryOnConnectionFailure(false)`. This allows the Box SDK to handle retries instead of OkHttp. +Here's an example implementation: +```java +import com.box.sdk.BoxAPIConnection; +import com.box.sdk.BoxConfig; +import okhttp3.OkHttpClient; + +public class CustomBoxAPIConnection extends BoxAPIConnection { + + public CustomBoxAPIConnection(BoxConfig boxConfig) { + super(boxConfig); + } + + @Override + protected OkHttpClient.Builder modifyHttpClientBuilder(OkHttpClient.Builder httpClientBuilder) { + httpClientBuilder.retryOnConnectionFailure(false); // Disable OkHttp retries + return httpClientBuilder; + } +} + +``` From ec03ba8ea43c7cb707979bc1624b60ec7ac94995 Mon Sep 17 00:00:00 2001 From: Hawra2020 Date: Tue, 14 Oct 2025 11:53:45 +0200 Subject: [PATCH 2/2] Made changes --- docs/sdk/configuration.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/sdk/configuration.md b/docs/sdk/configuration.md index d38030674..40c7be4df 100644 --- a/docs/sdk/configuration.md +++ b/docs/sdk/configuration.md @@ -15,6 +15,7 @@ - [Revoke URL](#revoke-url-deprecated) - [SSL configuration](#ssl-configuration) - [Instrumenation of OpenTelemetry](#instrumenation-of-opentelemetry) +- [OkHttp Default Retry Behavior](#okhttp-default-retry-behavior) # Proxy configuration