diff --git a/android/capacitor/src/main/assets/native-bridge.js b/android/capacitor/src/main/assets/native-bridge.js index 8d458087c1..7cee6e1db2 100644 --- a/android/capacitor/src/main/assets/native-bridge.js +++ b/android/capacitor/src/main/assets/native-bridge.js @@ -65,22 +65,29 @@ var nativeBridge = (function (exports) { return newFormData; }; const convertBody = async (body, contentType) => { - if (body instanceof ReadableStream) { - const reader = body.getReader(); - const chunks = []; - while (true) { - const { done, value } = await reader.read(); - if (done) - break; - chunks.push(value); + if (body instanceof ReadableStream || body instanceof Uint8Array) { + let encodedData; + if (body instanceof ReadableStream) { + const reader = body.getReader(); + const chunks = []; + while (true) { + const { done, value } = await reader.read(); + if (done) + break; + chunks.push(value); + } + const concatenated = new Uint8Array(chunks.reduce((acc, chunk) => acc + chunk.length, 0)); + let position = 0; + for (const chunk of chunks) { + concatenated.set(chunk, position); + position += chunk.length; + } + encodedData = concatenated; } - const concatenated = new Uint8Array(chunks.reduce((acc, chunk) => acc + chunk.length, 0)); - let position = 0; - for (const chunk of chunks) { - concatenated.set(chunk, position); - position += chunk.length; + else { + encodedData = body; } - let data = new TextDecoder().decode(concatenated); + let data = new TextDecoder().decode(encodedData); let type; if (contentType === 'application/json') { try { diff --git a/core/native-bridge.ts b/core/native-bridge.ts index 5bd70017b5..cb415519e9 100644 --- a/core/native-bridge.ts +++ b/core/native-bridge.ts @@ -55,24 +55,30 @@ const convertBody = async ( body: Document | XMLHttpRequestBodyInit | ReadableStream | undefined, contentType?: string, ): Promise => { - if (body instanceof ReadableStream) { - const reader = body.getReader(); - const chunks: any[] = []; - while (true) { - const { done, value } = await reader.read(); - if (done) break; - chunks.push(value); - } - const concatenated = new Uint8Array( - chunks.reduce((acc, chunk) => acc + chunk.length, 0), - ); - let position = 0; - for (const chunk of chunks) { - concatenated.set(chunk, position); - position += chunk.length; + if (body instanceof ReadableStream || body instanceof Uint8Array) { + let encodedData; + if (body instanceof ReadableStream) { + const reader = body.getReader(); + const chunks: any[] = []; + while (true) { + const { done, value } = await reader.read(); + if (done) break; + chunks.push(value); + } + const concatenated = new Uint8Array( + chunks.reduce((acc, chunk) => acc + chunk.length, 0), + ); + let position = 0; + for (const chunk of chunks) { + concatenated.set(chunk, position); + position += chunk.length; + } + encodedData = concatenated; + } else { + encodedData = body; } - let data = new TextDecoder().decode(concatenated); + let data = new TextDecoder().decode(encodedData); let type; if (contentType === 'application/json') { try { diff --git a/ios/Capacitor/Capacitor/assets/native-bridge.js b/ios/Capacitor/Capacitor/assets/native-bridge.js index 8d458087c1..7cee6e1db2 100644 --- a/ios/Capacitor/Capacitor/assets/native-bridge.js +++ b/ios/Capacitor/Capacitor/assets/native-bridge.js @@ -65,22 +65,29 @@ var nativeBridge = (function (exports) { return newFormData; }; const convertBody = async (body, contentType) => { - if (body instanceof ReadableStream) { - const reader = body.getReader(); - const chunks = []; - while (true) { - const { done, value } = await reader.read(); - if (done) - break; - chunks.push(value); + if (body instanceof ReadableStream || body instanceof Uint8Array) { + let encodedData; + if (body instanceof ReadableStream) { + const reader = body.getReader(); + const chunks = []; + while (true) { + const { done, value } = await reader.read(); + if (done) + break; + chunks.push(value); + } + const concatenated = new Uint8Array(chunks.reduce((acc, chunk) => acc + chunk.length, 0)); + let position = 0; + for (const chunk of chunks) { + concatenated.set(chunk, position); + position += chunk.length; + } + encodedData = concatenated; } - const concatenated = new Uint8Array(chunks.reduce((acc, chunk) => acc + chunk.length, 0)); - let position = 0; - for (const chunk of chunks) { - concatenated.set(chunk, position); - position += chunk.length; + else { + encodedData = body; } - let data = new TextDecoder().decode(concatenated); + let data = new TextDecoder().decode(encodedData); let type; if (contentType === 'application/json') { try {