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(http): properly write form-urlencoded data on android request body #7130

Merged
merged 1 commit into from
Dec 14, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,14 @@ public void setRequestBody(PluginCall call, JSValue body, String bodyType) throw
}
os.flush();
}
} else if (contentType.contains("application/x-www-form-urlencoded")) {
try {
JSObject obj = body.toJSObject();
this.writeObjectRequestBody(obj);
} catch (Exception e) {
// Body is not a valid JSON, treat it as an already formatted string
this.writeRequestBody(body.toString());
}
} else if (bodyType != null && bodyType.equals("formData")) {
this.writeFormDataRequestBody(contentType, body.toJSArray());
} else {
Expand All @@ -234,6 +242,24 @@ private void writeRequestBody(String body) throws IOException {
}
}

private void writeObjectRequestBody(JSObject object) throws IOException, JSONException {
try (DataOutputStream os = new DataOutputStream(connection.getOutputStream())) {
Iterator<String> keys = object.keys();
while (keys.hasNext()) {
String key = keys.next();
Object d = object.get(key);
os.writeBytes(key);
os.writeBytes("=");
os.writeBytes(URLEncoder.encode(d.toString(), "UTF-8"));

if (keys.hasNext()) {
os.writeBytes("&");
}
}
os.flush();
}
}

private void writeFormDataRequestBody(String contentType, JSArray entries) throws IOException, JSONException {
try (DataOutputStream os = new DataOutputStream(connection.getOutputStream())) {
String boundary = contentType.split(";")[1].split("=")[1];
Expand Down
Loading