fix: forward multipart/form-data correctly in import API proxy
The SvelteKit proxy was calling request.json() unconditionally, consuming the body before forwarding. File uploads (multipart/form-data) now use request.formData() and pass the FormData directly to backendFetch so the fetch API sets the correct Content-Type boundary automatically.
This commit is contained in:
@@ -17,22 +17,41 @@ export const GET: RequestHandler = async ({ locals }) => {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* POST /api/admin/import
|
* POST /api/admin/import
|
||||||
* Create a new import task.
|
* Create a new import task. Supports both multipart/form-data (file upload)
|
||||||
|
* and application/json (object key reference).
|
||||||
*/
|
*/
|
||||||
export const POST: RequestHandler = async ({ request, locals }) => {
|
export const POST: RequestHandler = async ({ request, locals }) => {
|
||||||
if (!locals.user || locals.user.role !== 'admin') {
|
if (!locals.user || locals.user.role !== 'admin') {
|
||||||
throw error(403, 'Forbidden');
|
throw error(403, 'Forbidden');
|
||||||
}
|
}
|
||||||
const body = await request.json();
|
|
||||||
const res = await backendFetch('/api/admin/import', {
|
const ct = request.headers.get('content-type') ?? '';
|
||||||
method: 'POST',
|
|
||||||
headers: { 'Content-Type': 'application/json' },
|
let res: Response;
|
||||||
body: JSON.stringify(body)
|
|
||||||
});
|
if (ct.includes('multipart/form-data')) {
|
||||||
|
// Forward the raw FormData body; let the browser-set Content-Type
|
||||||
|
// (which includes the boundary) pass through unchanged.
|
||||||
|
const formData = await request.formData();
|
||||||
|
res = await backendFetch('/api/admin/import', {
|
||||||
|
method: 'POST',
|
||||||
|
// Do NOT set Content-Type manually — the fetch API sets it
|
||||||
|
// automatically with the correct boundary when given a FormData body.
|
||||||
|
body: formData
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
const body = await request.json();
|
||||||
|
res = await backendFetch('/api/admin/import', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify(body)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
if (!res.ok) {
|
if (!res.ok) {
|
||||||
const err = await res.json().catch(() => ({ error: 'Failed to create import task' }));
|
const err = await res.json().catch(() => ({ error: 'Failed to create import task' }));
|
||||||
throw error(res.status, err.error || 'Failed to create import task');
|
throw error(res.status, err.error || 'Failed to create import task');
|
||||||
}
|
}
|
||||||
const data = await res.json();
|
const data = await res.json();
|
||||||
return json(data);
|
return json(data);
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user