From 07b3dda6fa78a603393890672b640e89dacafb9c Mon Sep 17 00:00:00 2001 From: Randall Stillwell Date: Mon, 21 Jul 2025 21:11:13 -0500 Subject: [PATCH] Fix Vercel function request body parsing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 🐛 Bug Fix: - Replace req.json() with req.text() + JSON.parse() for Vercel compatibility - Add proper error handling for malformed JSON requests - Fix authentication endpoints (login/register) body parsing - Fix admin users endpoint body parsing 🔧 Technical Details: - Vercel functions don't support req.json() method directly - Use req.text() to get raw body content then parse manually - Add try/catch blocks for JSON parsing errors - Maintain same API interface and error responses ✅ Endpoints Fixed: - /api/auth/register - User registration - /api/auth/login - User authentication - /api/admin/users - Admin user management This resolves the 'req.json is not a function' error in production. --- api/admin/users.ts | 15 ++++++++++++++- api/auth/login.ts | 15 ++++++++++++++- api/auth/register.ts | 15 ++++++++++++++- 3 files changed, 42 insertions(+), 3 deletions(-) diff --git a/api/admin/users.ts b/api/admin/users.ts index 47583d5..98c448d 100644 --- a/api/admin/users.ts +++ b/api/admin/users.ts @@ -105,7 +105,20 @@ export default async function handler(req: NextRequest) { }); } - const body = await req.json(); + // Parse request body + let body: { isActive?: boolean; roles?: string[] }; + try { + const bodyText = await req.text(); + body = JSON.parse(bodyText); + } catch (parseError) { + return new NextResponse(JSON.stringify({ + error: 'Invalid JSON in request body' + }), { + status: 400, + headers: { 'Content-Type': 'application/json' }, + }); + } + const { isActive, roles } = body; // Update user status diff --git a/api/auth/login.ts b/api/auth/login.ts index 69f0e2a..b97d823 100644 --- a/api/auth/login.ts +++ b/api/auth/login.ts @@ -24,7 +24,20 @@ export default async function handler(req: NextRequest) { const client = await pool.connect(); try { - const body: LoginRequest = await req.json(); + // Parse request body + let body: LoginRequest; + try { + const bodyText = await req.text(); + body = JSON.parse(bodyText); + } catch (parseError) { + return new NextResponse(JSON.stringify({ + error: 'Invalid JSON in request body' + }), { + status: 400, + headers: { 'Content-Type': 'application/json' }, + }); + } + const { username, password } = body; // Validate input diff --git a/api/auth/register.ts b/api/auth/register.ts index 5f49fe7..1c2bc30 100644 --- a/api/auth/register.ts +++ b/api/auth/register.ts @@ -27,7 +27,20 @@ export default async function handler(req: NextRequest) { const client = await pool.connect(); try { - const body: RegisterRequest = await req.json(); + // Parse request body + let body: RegisterRequest; + try { + const bodyText = await req.text(); + body = JSON.parse(bodyText); + } catch (parseError) { + return new NextResponse(JSON.stringify({ + error: 'Invalid JSON in request body' + }), { + status: 400, + headers: { 'Content-Type': 'application/json' }, + }); + } + const { username, email, password, firstName, lastName } = body; // Validate input