From 4247755e5f124b31bc9e792d8f7006376a6c741e Mon Sep 17 00:00:00 2001 From: ItsChaceD Date: Thu, 16 Feb 2023 07:52:17 -0800 Subject: [PATCH 1/5] fix(http): fix headers not being correctly set --- android/capacitor/src/main/assets/native-bridge.js | 6 +++--- core/native-bridge.ts | 9 +++++---- .../Capacitor/Plugins/CapacitorUrlRequest.swift | 2 +- ios/Capacitor/Capacitor/assets/native-bridge.js | 6 +++--- 4 files changed, 12 insertions(+), 11 deletions(-) diff --git a/android/capacitor/src/main/assets/native-bridge.js b/android/capacitor/src/main/assets/native-bridge.js index ee587328e1..0f2b0b6e46 100644 --- a/android/capacitor/src/main/assets/native-bridge.js +++ b/android/capacitor/src/main/assets/native-bridge.js @@ -2,7 +2,7 @@ /*! Capacitor: https://capacitorjs.com/ - MIT License */ /* Generated File. Do not edit. */ -var nativeBridge = (function (exports) { +const nativeBridge = (function (exports) { 'use strict'; var ExceptionCode; @@ -379,7 +379,7 @@ var nativeBridge = (function (exports) { method: (options === null || options === void 0 ? void 0 : options.method) ? options.method : undefined, data: (options === null || options === void 0 ? void 0 : options.body) ? options.body : undefined, headers: (options === null || options === void 0 ? void 0 : options.headers) - ? JSON.stringify(options.headers) + ? options.headers : undefined, }); const data = typeof nativeResponse.data === 'string' @@ -508,7 +508,7 @@ var nativeBridge = (function (exports) { url: this._url, method: this._method, data: body !== null ? body : undefined, - headers: JSON.stringify(this._headers), + headers: this._headers != null && Object.keys(this._headers).length > 0 ? this._headers : undefined, }) .then((nativeResponse) => { // intercept & parse response before returning diff --git a/core/native-bridge.ts b/core/native-bridge.ts index 6b5e0ee913..8608eaaf34 100644 --- a/core/native-bridge.ts +++ b/core/native-bridge.ts @@ -429,9 +429,7 @@ const initBridge = (w: any): void => { url: resource, method: options?.method ? options.method : undefined, data: options?.body ? options.body : undefined, - headers: options?.headers - ? JSON.stringify(options.headers) - : undefined, + headers: options?.headers ? options.headers : undefined, }, ); @@ -592,7 +590,10 @@ const initBridge = (w: any): void => { url: this._url, method: this._method, data: body !== null ? body : undefined, - headers: JSON.stringify(this._headers), + headers: + this._headers != null && Object.keys(this._headers).length > 0 + ? this._headers + : undefined, }) .then((nativeResponse: any) => { // intercept & parse response before returning diff --git a/ios/Capacitor/Capacitor/Plugins/CapacitorUrlRequest.swift b/ios/Capacitor/Capacitor/Plugins/CapacitorUrlRequest.swift index 745e11698a..3a886d1d9c 100644 --- a/ios/Capacitor/Capacitor/Plugins/CapacitorUrlRequest.swift +++ b/ios/Capacitor/Capacitor/Plugins/CapacitorUrlRequest.swift @@ -111,7 +111,7 @@ public class CapacitorUrlRequest: NSObject, URLSessionTaskDelegate { public func setRequestHeaders(_ headers: [String: String]) { headers.keys.forEach { (key: String) in let value = headers[key] - request.addValue(value!, forHTTPHeaderField: key) + request.setValue(value!, forHTTPHeaderField: key) self.headers[key] = value } } diff --git a/ios/Capacitor/Capacitor/assets/native-bridge.js b/ios/Capacitor/Capacitor/assets/native-bridge.js index ee587328e1..0f2b0b6e46 100644 --- a/ios/Capacitor/Capacitor/assets/native-bridge.js +++ b/ios/Capacitor/Capacitor/assets/native-bridge.js @@ -2,7 +2,7 @@ /*! Capacitor: https://capacitorjs.com/ - MIT License */ /* Generated File. Do not edit. */ -var nativeBridge = (function (exports) { +const nativeBridge = (function (exports) { 'use strict'; var ExceptionCode; @@ -379,7 +379,7 @@ var nativeBridge = (function (exports) { method: (options === null || options === void 0 ? void 0 : options.method) ? options.method : undefined, data: (options === null || options === void 0 ? void 0 : options.body) ? options.body : undefined, headers: (options === null || options === void 0 ? void 0 : options.headers) - ? JSON.stringify(options.headers) + ? options.headers : undefined, }); const data = typeof nativeResponse.data === 'string' @@ -508,7 +508,7 @@ var nativeBridge = (function (exports) { url: this._url, method: this._method, data: body !== null ? body : undefined, - headers: JSON.stringify(this._headers), + headers: this._headers != null && Object.keys(this._headers).length > 0 ? this._headers : undefined, }) .then((nativeResponse) => { // intercept & parse response before returning From f117ed3d15b080ff02e536166560433ff1a6cc1a Mon Sep 17 00:00:00 2001 From: ItsChaceD Date: Thu, 16 Feb 2023 08:05:00 -0800 Subject: [PATCH 2/5] fix(http): cast to string before setting header on ios --- ios/Capacitor/Capacitor/Plugins/CapacitorUrlRequest.swift | 6 +++--- ios/Capacitor/Capacitor/Plugins/HttpRequestHandler.swift | 3 +-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/ios/Capacitor/Capacitor/Plugins/CapacitorUrlRequest.swift b/ios/Capacitor/Capacitor/Plugins/CapacitorUrlRequest.swift index 3a886d1d9c..909e97ca9d 100644 --- a/ios/Capacitor/Capacitor/Plugins/CapacitorUrlRequest.swift +++ b/ios/Capacitor/Capacitor/Plugins/CapacitorUrlRequest.swift @@ -108,11 +108,11 @@ public class CapacitorUrlRequest: NSObject, URLSessionTaskDelegate { } } - public func setRequestHeaders(_ headers: [String: String]) { + public func setRequestHeaders(_ headers: [String: Any]) { headers.keys.forEach { (key: String) in let value = headers[key] - request.setValue(value!, forHTTPHeaderField: key) - self.headers[key] = value + request.setValue("\(value!)", forHTTPHeaderField: key) + self.headers[key] = "\(value!)" } } diff --git a/ios/Capacitor/Capacitor/Plugins/HttpRequestHandler.swift b/ios/Capacitor/Capacitor/Plugins/HttpRequestHandler.swift index 40cef58dc6..af73d570fa 100644 --- a/ios/Capacitor/Capacitor/Plugins/HttpRequestHandler.swift +++ b/ios/Capacitor/Capacitor/Plugins/HttpRequestHandler.swift @@ -144,8 +144,7 @@ class HttpRequestHandler { guard var urlString = call.getString("url") else { throw URLError(.badURL) } let method = httpMethod ?? call.getString("method", "GET") - // swiftlint:disable force_cast - let headers = (call.getObject("headers") ?? [:]) as! [String: String] + let headers = (call.getObject("headers") ?? [:]) as [String: Any] let params = (call.getObject("params") ?? [:]) as [String: Any] let responseType = call.getString("responseType") ?? "text" let connectTimeout = call.getDouble("connectTimeout") From 0acef19aef1580f6b5e77b7e06f8c26d5c87e867 Mon Sep 17 00:00:00 2001 From: ItsChaceD Date: Mon, 20 Feb 2023 15:34:05 -0800 Subject: [PATCH 3/5] deprecate old setRequestHeaders --- .../Capacitor/Plugins/CapacitorUrlRequest.swift | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/ios/Capacitor/Capacitor/Plugins/CapacitorUrlRequest.swift b/ios/Capacitor/Capacitor/Plugins/CapacitorUrlRequest.swift index 909e97ca9d..ea3e9ea3fd 100644 --- a/ios/Capacitor/Capacitor/Plugins/CapacitorUrlRequest.swift +++ b/ios/Capacitor/Capacitor/Plugins/CapacitorUrlRequest.swift @@ -108,6 +108,15 @@ public class CapacitorUrlRequest: NSObject, URLSessionTaskDelegate { } } + @available(*, deprecated) + public func setRequestHeaders(_ headers: [String: String]) { + headers.keys.forEach { (key: String) in + let value = headers[key] + request.setValue(value!, forHTTPHeaderField: key) + self.headers[key] = value + } + } + public func setRequestHeaders(_ headers: [String: Any]) { headers.keys.forEach { (key: String) in let value = headers[key] From 39f6ea2f707dc7a54b7d294378d49399597704a7 Mon Sep 17 00:00:00 2001 From: Mike Summerfeldt <20338451+IT-MikeS@users.noreply.github.com> Date: Tue, 21 Feb 2023 12:05:51 -0500 Subject: [PATCH 4/5] Update ios/Capacitor/Capacitor/Plugins/CapacitorUrlRequest.swift Co-authored-by: jcesarmobile --- ios/Capacitor/Capacitor/Plugins/CapacitorUrlRequest.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ios/Capacitor/Capacitor/Plugins/CapacitorUrlRequest.swift b/ios/Capacitor/Capacitor/Plugins/CapacitorUrlRequest.swift index ea3e9ea3fd..059bbfd976 100644 --- a/ios/Capacitor/Capacitor/Plugins/CapacitorUrlRequest.swift +++ b/ios/Capacitor/Capacitor/Plugins/CapacitorUrlRequest.swift @@ -112,7 +112,7 @@ public class CapacitorUrlRequest: NSObject, URLSessionTaskDelegate { public func setRequestHeaders(_ headers: [String: String]) { headers.keys.forEach { (key: String) in let value = headers[key] - request.setValue(value!, forHTTPHeaderField: key) + request.addValue(value!, forHTTPHeaderField: key) self.headers[key] = value } } From d4a208c7f6d4e6bc5ce59f9a448fb43e70f6efb0 Mon Sep 17 00:00:00 2001 From: IT-MikeS <20338451+IT-MikeS@users.noreply.github.com> Date: Tue, 21 Feb 2023 12:16:50 -0500 Subject: [PATCH 5/5] chore: add deprecation message --- ios/Capacitor/Capacitor/Plugins/CapacitorUrlRequest.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ios/Capacitor/Capacitor/Plugins/CapacitorUrlRequest.swift b/ios/Capacitor/Capacitor/Plugins/CapacitorUrlRequest.swift index 059bbfd976..0b571b0706 100644 --- a/ios/Capacitor/Capacitor/Plugins/CapacitorUrlRequest.swift +++ b/ios/Capacitor/Capacitor/Plugins/CapacitorUrlRequest.swift @@ -108,7 +108,7 @@ public class CapacitorUrlRequest: NSObject, URLSessionTaskDelegate { } } - @available(*, deprecated) + @available(*, deprecated, message: "Use newer function with passed headers of type [String: Any]") public func setRequestHeaders(_ headers: [String: String]) { headers.keys.forEach { (key: String) in let value = headers[key]