Skip to content
This repository has been archived by the owner on Sep 26, 2022. It is now read-only.

feat(web/ios): allow string value in body #196

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
c4bdd09
Add data to the request if they are present. Don't fail based on the …
shanselm-ergon Jul 20, 2021
aa6d19e
Update README
graefenhain Jul 27, 2021
2538caf
Merge branch 'capacitor-community:master' into master
shanselm-ergon Aug 15, 2021
0897e49
Update HttpRequestHandler.swift
shanselm-ergon Aug 27, 2021
0f86c5a
Update HttpRequestHandler.swift
shanselm-ergon Aug 27, 2021
9ede4b9
Merge remote-tracking branch 'origin/master' into ios-fix-required-bo…
shanselm-ergon Sep 24, 2021
92d666f
Replace throw with reject for safety reasons
shanselm-ergon Sep 24, 2021
04a4b27
Add data to the request if they are present. Don't fail based on the …
shanselm-ergon Jul 20, 2021
6071028
Update HttpRequestHandler.swift
shanselm-ergon Aug 27, 2021
451a843
Update HttpRequestHandler.swift
shanselm-ergon Aug 27, 2021
af05612
fix(iOS): pass http method to request functions on ios (#152)
joeflateau Jul 27, 2021
46d5e7e
chore: release 1.1.1
thomasvidas Jul 27, 2021
a9da0b7
release: 1.1.2
thomasvidas Jul 28, 2021
8f02185
refactor(android): JavaDoc and code cleanup
thomasvidas Jul 29, 2021
10605f6
fix(iOS): Add "getCookie" as plugin method for ios (#156)
FelixSchwarzmeier Aug 11, 2021
6e94465
feat: Add support for disabling automatic HTTP redirects on android/i…
FelixSchwarzmeier Aug 11, 2021
29383a9
fix: default content-type consistent with native impl (#158)
emily-curry Aug 11, 2021
876d8db
release: 1.2.0
thomasvidas Aug 11, 2021
6060734
Replace throw with reject for safety reasons
shanselm-ergon Sep 24, 2021
5f85399
Merge branch 'ios-fix-required-body-handling' of https://github.com/e…
shanselm-ergon Sep 24, 2021
93c1c67
Merge branch 'capacitor-community:master' into ios-fix-required-body-…
shanselm-ergon Sep 24, 2021
a40ecde
Revert "Update README"
shanselm-ergon Sep 24, 2021
6a6d083
Merge remote-tracking branch 'origin/master' into ios-fix-required-bo…
shanselm-ergon Sep 24, 2021
8b765cc
feat(web): pass-through string-type requests
emily-curry Nov 9, 2021
701183f
fix(ios): fix crash on json serialization failure
emily-curry Nov 9, 2021
f60ebec
feat(ios): write string body as-is
emily-curry Nov 9, 2021
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
15 changes: 11 additions & 4 deletions ios/Plugin/CapacitorUrlRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@ public class CapacitorUrlRequest: NSObject, URLSessionTaskDelegate {
}

private func getRequestDataAsJson(_ data: JSValue) throws -> Data? {
let jsonData = try JSONSerialization.data(withJSONObject: data)
return jsonData
// We need to check if the JSON is valid before attempting to serialize, as JSONSerialization.data will not throw an exception that can be caught, and will cause the application to crash if it fails.
if JSONSerialization.isValidJSONObject(data) {
return try JSONSerialization.data(withJSONObject: data)
} else {
throw CapacitorUrlRequest.CapacitorUrlRequestError.serializationError("[ data ] argument for request of content-type [ application/json ] must be serializable to JSON")
}
}

private func getRequestDataAsFormUrlEncoded(_ data: JSValue) throws -> Data? {
Expand Down Expand Up @@ -84,14 +88,17 @@ public class CapacitorUrlRequest: NSObject, URLSessionTaskDelegate {
}

func getRequestData(_ body: JSValue, _ contentType: String) throws -> Data? {
if contentType.contains("application/json") {
// If data can be parsed directly as a string, return that without processing.
if let strVal = try? getRequestDataAsString(body) {
return strVal
} else if contentType.contains("application/json") {
return try getRequestDataAsJson(body)
} else if contentType.contains("application/x-www-form-urlencoded") {
return try getRequestDataAsFormUrlEncoded(body)
} else if contentType.contains("multipart/form-data") {
return try getRequestDataAsMultipartFormData(body)
} else {
return try getRequestDataAsString(body)
throw CapacitorUrlRequestError.serializationError("[ data ] argument could not be parsed for content type [ \(contentType) ]")
}
}

Expand Down
17 changes: 3 additions & 14 deletions ios/Plugin/HttpRequestHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,6 @@ class HttpRequestHandler {
let connectTimeout = call.getDouble("connectTimeout");
let readTimeout = call.getDouble("readTimeout");

let isHttpMutate = method == "DELETE" ||
method == "PATCH" ||
method == "POST" ||
method == "PUT";

let request = try! CapacitorHttpRequestBuilder()
.setUrl(urlString)
.setMethod(method)
Expand All @@ -178,19 +173,14 @@ class HttpRequestHandler {
let timeout = (connectTimeout ?? readTimeout ?? 600000.0) / 1000.0;
request.setTimeout(timeout)

if isHttpMutate {
if let data = call.options["data"] as? JSValue {
do {
guard let data = call.jsObjectRepresentation["data"]
else {
throw CapacitorUrlRequest.CapacitorUrlRequestError.serializationError("Invalid [ data ] argument")
}

try request.setRequestBody(data)
} catch {
// Explicitly reject if the http request body was not set successfully,
// so as to not send a known malformed request, and to provide the developer with additional context.
call.reject("Error", "REQUEST", error, [:])
return;
return
}
}

Expand All @@ -199,8 +189,7 @@ class HttpRequestHandler {
let task = urlSession.dataTask(with: urlRequest) { (data, response, error) in
urlSession.invalidateAndCancel();
if error != nil {
call.reject("Error", "REQUEST", error, [:])
return;
return
}

let type = ResponseType(rawValue: responseType) ?? .default
Expand Down
6 changes: 5 additions & 1 deletion src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,12 @@ export const buildRequestInit = (
const headers = normalizeHttpHeaders(options.headers);
const type = headers['content-type'] || '';

// If body is already a string, then pass it through as-is.
if (typeof options.data === 'string') {
output.body = options.data;
}
// Build request initializers based off of content-type
if (type.includes('application/x-www-form-urlencoded')) {
else if (type.includes('application/x-www-form-urlencoded')) {
const params = new URLSearchParams();
for (const [key, value] of Object.entries(options.data || {})) {
params.set(key, value as any);
Expand Down