Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ios): Correctly Attach Headers to Request #6303

Merged
merged 6 commits into from
Feb 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions android/capacitor/src/main/assets/native-bridge.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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]) {
ItsChaceD marked this conversation as resolved.
Show resolved Hide resolved
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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