Skip to content

Commit 87b4641

Browse files
fix(http): Properly URL-encode key and values during x-www-form-urlencoded POSTs (#8037)
Co-authored-by: jcesarmobile <jcesarmobile@gmail.com>
1 parent 591c6a4 commit 87b4641

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

android/capacitor/src/main/java/com/getcapacitor/plugin/util/CapacitorHttpUrlConnection.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ private void writeObjectRequestBody(JSObject object) throws IOException, JSONExc
248248
while (keys.hasNext()) {
249249
String key = keys.next();
250250
Object d = object.get(key);
251-
os.writeBytes(key);
251+
os.writeBytes(URLEncoder.encode(key, "UTF-8"));
252252
os.writeBytes("=");
253253
os.writeBytes(URLEncoder.encode(d.toString(), "UTF-8"));
254254

ios/Capacitor/Capacitor/Plugins/CapacitorUrlRequest.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,11 @@ open class CapacitorUrlRequest: NSObject, URLSessionTaskDelegate {
4040
throw CapacitorUrlRequestError.serializationError("[ data ] argument for request with content-type [ multipart/form-data ] may only be a plain javascript object")
4141
}
4242

43+
let allowed = CharacterSet(charactersIn: "-._*").union(.alphanumerics)
44+
4345
obj.keys.forEach { (key: String) in
44-
components.queryItems?.append(URLQueryItem(name: key, value: "\(obj[key] ?? "")"))
46+
let value = obj[key] as? String ?? ""
47+
components.queryItems?.append(URLQueryItem(name: key.addingPercentEncoding(withAllowedCharacters: allowed)?.replacingOccurrences(of: "%20", with: "+") ?? key, value: value.addingPercentEncoding(withAllowedCharacters: allowed)?.replacingOccurrences(of: "%20", with: "+")))
4548
}
4649

4750
if components.query != nil {

0 commit comments

Comments
 (0)