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,18 +17,37 @@ export const GET: RequestHandler = async ({ locals }) => {
|
||||
|
||||
/**
|
||||
* 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 }) => {
|
||||
if (!locals.user || locals.user.role !== 'admin') {
|
||||
throw error(403, 'Forbidden');
|
||||
}
|
||||
const body = await request.json();
|
||||
const res = await backendFetch('/api/admin/import', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(body)
|
||||
});
|
||||
|
||||
const ct = request.headers.get('content-type') ?? '';
|
||||
|
||||
let res: Response;
|
||||
|
||||
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) {
|
||||
const err = await res.json().catch(() => ({ error: 'Failed to create import task' }));
|
||||
throw error(res.status, err.error || 'Failed to create import task');
|
||||
|
||||
Reference in New Issue
Block a user