From a3f875cf42e111fde07d6e87643264b19ed77573 Mon Sep 17 00:00:00 2001 From: Chace Daniels Date: Tue, 21 Feb 2023 11:22:59 -0800 Subject: [PATCH] fix(ios): Correctly Attach Headers to Request (#6303) --- android/capacitor/src/main/assets/native-bridge.js | 6 +++--- core/native-bridge.ts | 9 +++++---- .../Capacitor/Plugins/CapacitorUrlRequest.swift | 9 +++++++++ ios/Capacitor/Capacitor/Plugins/HttpRequestHandler.swift | 3 +-- ios/Capacitor/Capacitor/assets/native-bridge.js | 6 +++--- 5 files changed, 21 insertions(+), 12 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 359228f52d..bfe845b595 100644 --- a/ios/Capacitor/Capacitor/Plugins/CapacitorUrlRequest.swift +++ b/ios/Capacitor/Capacitor/Plugins/CapacitorUrlRequest.swift @@ -108,6 +108,7 @@ open class CapacitorUrlRequest: NSObject, URLSessionTaskDelegate { } } + @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] @@ -116,6 +117,14 @@ open class CapacitorUrlRequest: NSObject, URLSessionTaskDelegate { } } + 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!)" + } + } + public func setRequestBody(_ body: JSValue) throws { let contentType = self.getRequestHeader("Content-Type") as? String diff --git a/ios/Capacitor/Capacitor/Plugins/HttpRequestHandler.swift b/ios/Capacitor/Capacitor/Plugins/HttpRequestHandler.swift index 811ea3d801..f79b531d57 100644 --- a/ios/Capacitor/Capacitor/Plugins/HttpRequestHandler.swift +++ b/ios/Capacitor/Capacitor/Plugins/HttpRequestHandler.swift @@ -146,8 +146,7 @@ open 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") 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