diff --git a/android/capacitor/src/main/assets/native-bridge.js b/android/capacitor/src/main/assets/native-bridge.js index 9be99af24e..8c18f21317 100644 --- a/android/capacitor/src/main/assets/native-bridge.js +++ b/android/capacitor/src/main/assets/native-bridge.js @@ -424,22 +424,21 @@ var nativeBridge = (function (exports) { if (doPatchHttp) { // fetch patch window.fetch = async (resource, options) => { - if (!(resource.toString().startsWith('http:') || - resource.toString().startsWith('https:'))) { + const request = new Request(resource, options); + if (!(request.url.startsWith('http:') || + request.url.startsWith('https:'))) { return win.CapacitorWebFetch(resource, options); } const tag = `CapacitorHttp fetch ${Date.now()} ${resource}`; console.time(tag); try { // intercept request & pass to the bridge - const { data: requestData, type, headers, } = await convertBody((options === null || options === void 0 ? void 0 : options.body) || undefined); - let optionHeaders = options === null || options === void 0 ? void 0 : options.headers; - if ((options === null || options === void 0 ? void 0 : options.headers) instanceof Headers) { - optionHeaders = Object.fromEntries(options.headers.entries()); - } + const { body, method } = request; + const { data: requestData, type, headers, } = await convertBody(body || undefined); + const optionHeaders = Object.fromEntries(request.headers.entries()); const nativeResponse = await cap.nativePromise('CapacitorHttp', 'request', { - url: resource, - method: (options === null || options === void 0 ? void 0 : options.method) ? options.method : undefined, + url: request.url, + method: method, data: requestData, dataType: type, headers: Object.assign(Object.assign({}, headers), optionHeaders), diff --git a/core/native-bridge.ts b/core/native-bridge.ts index 89522d6212..8049030498 100644 --- a/core/native-bridge.ts +++ b/core/native-bridge.ts @@ -472,10 +472,12 @@ const initBridge = (w: any): void => { resource: RequestInfo | URL, options?: RequestInit, ) => { + const request = new Request(resource, options); + if ( !( - resource.toString().startsWith('http:') || - resource.toString().startsWith('https:') + request.url.startsWith('http:') || + request.url.startsWith('https:') ) ) { return win.CapacitorWebFetch(resource, options); @@ -485,23 +487,19 @@ const initBridge = (w: any): void => { console.time(tag); try { // intercept request & pass to the bridge + const { body, method } = request; const { data: requestData, type, headers, - } = await convertBody(options?.body || undefined); - let optionHeaders = options?.headers; - if (options?.headers instanceof Headers) { - optionHeaders = Object.fromEntries( - (options.headers as any).entries(), - ); - } + } = await convertBody(body || undefined); + const optionHeaders = Object.fromEntries(request.headers.entries()); const nativeResponse: HttpResponse = await cap.nativePromise( 'CapacitorHttp', 'request', { - url: resource, - method: options?.method ? options.method : undefined, + url: request.url, + method: method, data: requestData, dataType: type, headers: { diff --git a/ios/Capacitor/Capacitor/assets/native-bridge.js b/ios/Capacitor/Capacitor/assets/native-bridge.js index 9be99af24e..8c18f21317 100644 --- a/ios/Capacitor/Capacitor/assets/native-bridge.js +++ b/ios/Capacitor/Capacitor/assets/native-bridge.js @@ -424,22 +424,21 @@ var nativeBridge = (function (exports) { if (doPatchHttp) { // fetch patch window.fetch = async (resource, options) => { - if (!(resource.toString().startsWith('http:') || - resource.toString().startsWith('https:'))) { + const request = new Request(resource, options); + if (!(request.url.startsWith('http:') || + request.url.startsWith('https:'))) { return win.CapacitorWebFetch(resource, options); } const tag = `CapacitorHttp fetch ${Date.now()} ${resource}`; console.time(tag); try { // intercept request & pass to the bridge - const { data: requestData, type, headers, } = await convertBody((options === null || options === void 0 ? void 0 : options.body) || undefined); - let optionHeaders = options === null || options === void 0 ? void 0 : options.headers; - if ((options === null || options === void 0 ? void 0 : options.headers) instanceof Headers) { - optionHeaders = Object.fromEntries(options.headers.entries()); - } + const { body, method } = request; + const { data: requestData, type, headers, } = await convertBody(body || undefined); + const optionHeaders = Object.fromEntries(request.headers.entries()); const nativeResponse = await cap.nativePromise('CapacitorHttp', 'request', { - url: resource, - method: (options === null || options === void 0 ? void 0 : options.method) ? options.method : undefined, + url: request.url, + method: method, data: requestData, dataType: type, headers: Object.assign(Object.assign({}, headers), optionHeaders),