Skip to content

Commit f95815a

Browse files
feat: add Capacitor FormData implementation for testing multipart/form-data requests (#785)
1 parent 8601e07 commit f95815a

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

src/pages/Http.tsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ const HttpPage: React.FC = () => {
4141
sendRequestFetch();
4242
} else if (implementation === 'XHR') {
4343
sendRequestXHR();
44+
} else if (implementation === 'CAPACITOR_FORMDATA') {
45+
sendRequestCapacitorFormData();
4446
} else {
4547
sendRequestCapacitor();
4648
}
@@ -88,6 +90,24 @@ const HttpPage: React.FC = () => {
8890
}
8991
};
9092

93+
const sendRequestCapacitorFormData = async () => {
94+
const entries: { key: string; value: string; type: string }[] = [];
95+
if (params.length > 0) {
96+
const parsed = JSON.parse(params);
97+
for (const [key, value] of Object.entries(parsed)) {
98+
entries.push({ key, value: `${value}`, type: 'string' });
99+
}
100+
}
101+
const response = await CapacitorHttp.request({
102+
url,
103+
method: requestType,
104+
data: entries,
105+
dataType: 'formData',
106+
headers: JSON.parse(headers),
107+
});
108+
setResponse(JSON.stringify(response.data));
109+
};
110+
91111
const sendRequestCapacitor = async () => {
92112
const response = await CapacitorHttp.request({
93113
url,
@@ -152,6 +172,7 @@ const HttpPage: React.FC = () => {
152172
<IonSelectOption value="FETCH">Fetch</IonSelectOption>
153173
<IonSelectOption value="XHR">XMLHttpRequest</IonSelectOption>
154174
<IonSelectOption value="CAPACITOR">Capacitor</IonSelectOption>
175+
<IonSelectOption value="CAPACITOR_FORMDATA">Capacitor (FormData)</IonSelectOption>
155176
</IonSelect>
156177
<IonButton expand="block" onClick={() => toggleParams()}>
157178
Toggle Params On/Off

0 commit comments

Comments
 (0)