Fix multipart/form-data request payload previews on Android (#54804)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/54804

Resolves https://github.com/facebook/react-native/issues/54796#issuecomment-3616644916.

Fixes a bug where for `multipart/form-data` requests on Android, request payload previews were not using a correctly serialized preview string in React Native DevTools.

Changelog: [Internal]

Reviewed By: vzaidman

Differential Revision: D88495615

fbshipit-source-id: 757487b3588372404fa45f980e0bcfae5c7c6457
This commit is contained in:
Alex Hunt authored and meta-codesync[bot] committed 2025-12-08 02:15:37 -08:00
1 parent 2efa274c07
commit 38513f72d7
2 files changed
+22 -1

No files matched your search

@@ -35,7 +35,8 @@ internal object NetworkEventUtil {
request.url().toString(),
request.method(),
headersMap,
request.body()?.toString().orEmpty(),
(request.body() as? ProgressRequestBody)?.getBodyPreview()
?: request.body()?.toString().orEmpty(),
request.body()?.contentLength() ?: 0,
)
InspectorNetworkReporter.reportConnectionTiming(devToolsRequestId, headersMap)
@@ -23,6 +23,10 @@ internal class ProgressRequestBody(
) : RequestBody() {
private var contentLength = 0L
companion object {
private const val MAX_BODY_PREVIEW_SIZE = 1024 * 1024 // 1MB
}
override fun contentType(): MediaType? {
return requestBody.contentType()
}
@@ -78,4 +82,20 @@ internal class ProgressRequestBody(
}
)
}
fun getBodyPreview(): String {
return try {
val buffer = okio.Buffer()
requestBody.writeTo(buffer)
val size = buffer.size()
if (size <= MAX_BODY_PREVIEW_SIZE) {
buffer.readUtf8()
} else {
buffer.readUtf8(MAX_BODY_PREVIEW_SIZE.toLong()) +
"\n... [truncated, showing $MAX_BODY_PREVIEW_SIZE of $size bytes]"
}
} catch (e: Exception) {
""
}
}
}