fix: req.formData() should be used in some API routes (#19930)
This commit is contained in:
1 parent
2f9a853023
commit
517d509321
14 files changed
+59
-14
No files matched your search
@@ -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}`);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user