Repository navigation
Description
Capacitor Version
@capacitor/core: 8.6.2
@capacitor/android: 8.6.2
@capacitor/ios: 8.6.2
Other API Details
CapacitorHttp enabled in capacitor.config.json:
"plugins": { "CapacitorHttp": { "enabled": true } }
Tested with the linked repro: Android 14 emulator (API 34), iOS 27.2 simulator (Xcode 27.2).
Platforms Affected
- iOS
- Android
- Web
Current Behavior
With CapacitorHttp enabled, the patched fetch does not send binary request bodies as bytes. Same 8 bytes (1f8b0800ff00807f, gzip-like, not valid UTF-8) POSTed to an echo server:
| Request | Android 14 emulator (API 34) | iOS 27.2 simulator |
|---|---|---|
patched fetch, ArrayBuffer, application/octet-stream |
received {} |
error: CapacitorUrlRequestError error 0 |
patched fetch, Uint8Array, application/octet-stream |
received 1fefbfbd0800efbfbd00efbfbd7f (U+FFFD replacements) |
same as Android |
patched fetch, ArrayBuffer, application/json |
received {} |
received {} |
window.CapacitorWebFetch, ArrayBuffer (control) |
received 1f8b0800ff00807f |
received 1f8b0800ff00807f |
The cause is convertBody in native-bridge.js (same on iOS and Android):
ArrayBuffer(alsoBlob,DataView, typed arrays other thanUint8Array) matches no branch and falls through toreturn { data: body, type: 'json' }. TheArrayBufferis JSON-serialized on the way to native, which yields{}.Uint8Arrayis run throughnew TextDecoder().decode(...)(thenJSON.parseforapplication/json), so any non-UTF-8 bytes are replaced and the body is no longer the original bytes.
There is no way to opt a single request out of the patch other than calling window.CapacitorWebFetch directly.
Real-world impact: the Amplitude browser SDK (@amplitude/analytics-browser 2.x) gzips any upload of 2 KB or more to its default endpoints (compressToGzipArrayBuffer, sent as an ArrayBuffer with Content-Encoding: gzip). On native with CapacitorHttp enabled, every such upload is sent as {}, rejected with 400, and dropped after retries. That silently loses the larger analytics batches, typically the one sent at app start.
Related: #7471 was closed with "If you mean when using the patched fetch/XHR, it should be working already, if it's not working for you, please, create a new issue and provide a sample app". This is that issue.
Expected Behavior
Binary request bodies (ArrayBuffer, ArrayBufferView, Blob) sent through the patched fetch/XHR reach the server byte-for-byte, with the caller's Content-Type / Content-Encoding headers, the same as in a browser or with CapacitorHttp disabled. Uint8Array should be sent as bytes, not decoded as text.
Project Reproduction
https://github.com/mgoldsborough/capacitor-http-arraybuffer-repro
npm install && npx cap sync && npx cap run android (or ios), then tap Run. All app code is in www/index.html; the only configuration is CapacitorHttp.enabled: true. Core of it:
const bytes = new Uint8Array([0x1f, 0x8b, 0x08, 0x00, 0xff, 0x00, 0x80, 0x7f]);
const res = await fetch("https://httpbin.org/anything", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: bytes.buffer, // ArrayBuffer
});
console.log((await res.json()).data);
// Browser, CapacitorHttp disabled, or window.CapacitorWebFetch: the 8 bytes.
// Native with CapacitorHttp enabled: "{}".Additional Information
Workaround we use: route the affected requests to window.CapacitorWebFetch (the WebView's original fetch), which carries binary bodies.
A possible fix: in convertBody, handle ArrayBuffer / ArrayBuffer.isView(body) / Blob explicitly, for example by base64-encoding the bytes on the JS side and decoding them to raw bytes natively, and stop decoding Uint8Array as text unless the content type is textual.
Activity