Skip to content

Commit 4d93521

Browse files
committed
docs(boxsdk): fix 0 byte uploads being created on retry after application fails initla upload attempt
1 parent 3a02d44 commit 4d93521

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

docs/sdk/configuration.md

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,4 +326,31 @@ public class BoxAPIConnectionWithTelemetry extends BoxAPIConnection {
326326
```
327327

328328
Please note that you should not modify either `httpClient` or `request` parameters in the createNewCall method.
329-
Altering these parameters can discard the BoxAPIConnection configuration and lead to unexpected behavior.
329+
Altering these parameters can discard the BoxAPIConnection configuration and lead to unexpected behavior.
330+
331+
# OkHttp Default Retry Behavior
332+
333+
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.
334+
335+
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.
336+
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.
337+
Here's an example implementation:
338+
```java
339+
import com.box.sdk.BoxAPIConnection;
340+
import com.box.sdk.BoxConfig;
341+
import okhttp3.OkHttpClient;
342+
343+
public class CustomBoxAPIConnection extends BoxAPIConnection {
344+
345+
public CustomBoxAPIConnection(BoxConfig boxConfig) {
346+
super(boxConfig);
347+
}
348+
349+
@Override
350+
protected OkHttpClient.Builder modifyHttpClientBuilder(OkHttpClient.Builder httpClientBuilder) {
351+
httpClientBuilder.retryOnConnectionFailure(false); // Disable OkHttp retries
352+
return httpClientBuilder;
353+
}
354+
}
355+
356+
```

0 commit comments

Comments
 (0)