fix: req.formData() should be used in some API routes (#19930)

This commit is contained in:
Benny Joo authored and GitHub committed 2025-03-11 07:51:33 +00:00
1 parent 2f9a853023
commit 517d509321
14 files changed
+59 -14

No files matched your search

+19
View File
@@ -0,0 +1,19 @@
import type { NextRequest } from "next/server";
export async function parseRequestData(req: NextRequest): Promise<Record<string, any>> {
try {
return await req.json();
} catch (error) {
const contentType = req.headers.get("content-type") || "";
// Handle form data requests
if (
contentType.includes("application/x-www-form-urlencoded") ||
contentType.includes("multipart/form-data")
) {
const formData = await req.formData();
return Object.fromEntries(formData.entries());
}
throw new Error(`Unsupported content type: ${contentType}`);
}
}