Skip to content

Commit 459cac2

Browse files
authored
fix(http): boundary not added for Request objects (#7897) (#7904)
1 parent 03dcb5e commit 459cac2

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

android/capacitor/src/main/assets/native-bridge.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,15 @@ var nativeBridge = (function (exports) {
497497
if (doPatchHttp) {
498498
// fetch patch
499499
window.fetch = async (resource, options) => {
500+
const headers = new Headers(options === null || options === void 0 ? void 0 : options.headers);
501+
const contentType = headers.get('Content-Type') || headers.get('content-type');
502+
if ((options === null || options === void 0 ? void 0 : options.body) instanceof FormData &&
503+
(contentType === null || contentType === void 0 ? void 0 : contentType.includes('multipart/form-data')) &&
504+
!contentType.includes('boundary')) {
505+
headers.delete('Content-Type');
506+
headers.delete('content-type');
507+
options.headers = headers;
508+
}
500509
const request = new Request(resource, options);
501510
if (request.url.startsWith(`${cap.getServerUrl()}/`)) {
502511
return win.CapacitorWebFetch(resource, options);

core/native-bridge.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,18 @@ const initBridge = (w: any): void => {
543543
resource: RequestInfo | URL,
544544
options?: RequestInit,
545545
) => {
546+
const headers = new Headers(options?.headers);
547+
const contentType =
548+
headers.get('Content-Type') || headers.get('content-type');
549+
if (
550+
options?.body instanceof FormData &&
551+
contentType?.includes('multipart/form-data') &&
552+
!contentType.includes('boundary')
553+
) {
554+
headers.delete('Content-Type');
555+
headers.delete('content-type');
556+
options.headers = headers;
557+
}
546558
const request = new Request(resource, options);
547559
if (request.url.startsWith(`${cap.getServerUrl()}/`)) {
548560
return win.CapacitorWebFetch(resource, options);

ios/Capacitor/Capacitor/assets/native-bridge.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,15 @@ var nativeBridge = (function (exports) {
497497
if (doPatchHttp) {
498498
// fetch patch
499499
window.fetch = async (resource, options) => {
500+
const headers = new Headers(options === null || options === void 0 ? void 0 : options.headers);
501+
const contentType = headers.get('Content-Type') || headers.get('content-type');
502+
if ((options === null || options === void 0 ? void 0 : options.body) instanceof FormData &&
503+
(contentType === null || contentType === void 0 ? void 0 : contentType.includes('multipart/form-data')) &&
504+
!contentType.includes('boundary')) {
505+
headers.delete('Content-Type');
506+
headers.delete('content-type');
507+
options.headers = headers;
508+
}
500509
const request = new Request(resource, options);
501510
if (request.url.startsWith(`${cap.getServerUrl()}/`)) {
502511
return win.CapacitorWebFetch(resource, options);

0 commit comments

Comments
 (0)