Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Accept FormData / URLSearchParams as the request body directly (#75005)

Romeo Van Snick4f4134ebe978
diff --git a/enterprise/frontend/src/metabase-enterprise/api/advanced-config.ts b/enterprise/frontend/src/metabase-enterprise/api/advanced-config.ts
index d4ef243efea..fcf2e66bc7e 100644
--- a/enterprise/frontend/src/metabase-enterprise/api/advanced-config.ts
+++ b/enterprise/frontend/src/metabase-enterprise/api/advanced-config.ts
@@ -12,9 +12,7 @@ export const advancedConfigApi = EnterpriseApi.injectEndpoints({
return {
method: "POST",
url: "/api/ee/advanced-config",
- body: { formData },
- formData: true,
- fetch: true,
+ body: formData,
};
},
invalidatesTags: (_, error) =>
diff --git a/frontend/src/metabase/api/legacy-client.ts b/frontend/src/metabase/api/legacy-client.ts
index a1f9d172bce..bf7a81e210f 100644
--- a/frontend/src/metabase/api/legacy-client.ts
+++ b/frontend/src/metabase/api/legacy-client.ts
@@ -240,9 +240,16 @@ export class LegacyApi extends EventEmitter<EventMap> {
let body: string | FormData | undefined;
if (options.hasBody) {
- body = options.formData
- ? (rawData["formData"] as FormData)
- : JSON.stringify(data);
+ if (rawData instanceof FormData) {
+ // Pass `FormData` through unwrapped so the browser sets the right
+ // multipart Content-Type (with boundary). Drops the legacy
+ // `body: { formData }, formData: true` wrapper shape.
+ body = rawData;
+ } else if (options.formData) {
+ body = rawData["formData"] as FormData;
+ } else {
+ body = JSON.stringify(data);
+ }
} else {
const qs = querystring.stringify(data as Record<string, string>);
if (qs) {
@@ -258,7 +265,10 @@ export class LegacyApi extends EventEmitter<EventMap> {
...options.headers,
};
- if (options.formData && options.fetch) {
+ // FormData must NOT carry our default `application/json` Content-Type —
+ // the browser sets the right value (with the multipart boundary). XHR's
+ // `xhr.send(formData)` honors this too.
+ if (body instanceof FormData || (options.formData && options.fetch)) {
delete headers["Content-Type"];
}
diff --git a/frontend/src/metabase/api/query.ts b/frontend/src/metabase/api/query.ts
index 14aa3bb4fca..a90dda2bb0b 100644
--- a/frontend/src/metabase/api/query.ts
+++ b/frontend/src/metabase/api/query.ts
@@ -23,20 +23,24 @@ export const apiQuery: BaseQueryFn = async (args, ctx, extraOptions) => {
return { error: "Invalid HTTP method" };
}
+ // `FormData` bodies are forwarded as-is — spreading would yield an empty
+ // object (its entries aren't enumerable as keys) and erase the upload. Other
+ // bodies merge with `params` so a single combined object reaches the legacy
+ // client (which doesn't separate them).
+ const rawData =
+ args?.body instanceof FormData
+ ? args.body
+ : { ...args?.body, ...args?.params };
+
try {
- const response = await api[method](url)(
- // this will transform arrays to objects with numeric keys
- // we shouldn't be using top level-arrays in the API
- { ...args?.body, ...args?.params },
- {
- signal: ctx.signal,
- noEvent,
- formData,
- fetch,
- transformResponse,
- ...extraOptions,
- },
- );
+ const response = await api[method](url)(rawData, {
+ signal: ctx.signal,
+ noEvent,
+ formData,
+ fetch,
+ transformResponse,
+ ...extraOptions,
+ });
return { data: response };
} catch (error) {
return { error };
diff --git a/frontend/src/metabase/api/table.ts b/frontend/src/metabase/api/table.ts
index 28bd896d640..15b9ee59ff7 100644
--- a/frontend/src/metabase/api/table.ts
+++ b/frontend/src/metabase/api/table.ts
@@ -172,9 +172,7 @@ export const tableApi = Api.injectEndpoints({
query: ({ tableId, formData }) => ({
method: "POST",
url: `/api/table/${tableId}/append-csv`,
- body: { formData },
- formData: true,
- fetch: true,
+ body: formData,
}),
invalidatesTags: (_, error, { tableId }) =>
invalidateTags(error, [
@@ -191,9 +189,7 @@ export const tableApi = Api.injectEndpoints({
query: ({ tableId, formData }) => ({
method: "POST",
url: `/api/table/${tableId}/replace-csv`,
- body: { formData },
- formData: true,
- fetch: true,
+ body: formData,
}),
invalidatesTags: (_, error, { tableId }) =>
invalidateTags(error, [