Skip to content

Commit

Permalink
fix(ios): Correctly Attach Headers to Request (#6303)
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsChaceD committed Feb 21, 2023
1 parent 3fb3658 commit a3f875c
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 12 deletions.
6 changes: 3 additions & 3 deletions android/capacitor/src/main/assets/native-bridge.js
Expand Up @@ -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;
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions core/native-bridge.ts
Expand Up @@ -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,
},
);

Expand Down Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions ios/Capacitor/Capacitor/Plugins/CapacitorUrlRequest.swift
Expand Up @@ -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]
Expand All @@ -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

Expand Down
3 changes: 1 addition & 2 deletions ios/Capacitor/Capacitor/Plugins/HttpRequestHandler.swift
Expand Up @@ -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")
Expand Down
6 changes: 3 additions & 3 deletions ios/Capacitor/Capacitor/assets/native-bridge.js
Expand Up @@ -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;
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit a3f875c

Please sign in to comment.